Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF - Get Hyperlink command from data?
    primarykey
    data
    text
    <p>In WPF, how can I get a reference to the Command that a Hyperlink should invoke from an object property?</p> <p>I am creating a WPF application, using the MVVM pattern. A list box in the main window dislays hyperlinks. Each hyperlink will invoke one of the view model's ICommand properties when it is clicked. How do I specify which ICommand should be invoked?</p> <p>Here is what I have tried so far: The hyperlinks are contained in ViewModel.Hyperlinks property, which is bound as the ItemsSource for the list box. The Hyperlinks property contains objects of type MyHyperlink:</p> <pre><code>public class MyHyperlink { public string Text { get; set; } public string ViewModelCommand { get; set; } public int RecordID { get; set; } } </code></pre> <p>The MyHyperlink.ViewModelCommand property contains the name of the view model ICommand that should be invoked when the hyperlink is clicked. I want to use that value to specify a PropertyPath for the Command property of the WPF Hyperlink control . I tried creating a PropertyPath resource for the list box with the name of the command, but WPF won't accept that. Here is my XAML:</p> <pre><code>&lt;ListBox ItemsSource="{Binding Hyperlinks}"&gt; &lt;ListBox.Resources&gt; &lt;PropertyPath x:Key="CommandPath" Path="{Binding ViewModelCommand}" /&gt; &lt;/ListBox.Resources&gt; &lt;ListBox.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;TextBlock&gt; &lt;Hyperlink Command="{StaticResource CommandPath}" CommandParameter="{Binding Path=RecordID}"&gt; &lt;TextBlock Text="{Binding Text}" /&gt; &lt;/Hyperlink&gt; &lt;/TextBlock&gt; &lt;/DataTemplate&gt; &lt;/ListBox.ItemTemplate&gt; &lt;/ListBox&gt; </code></pre> <p>How do I specify which ICommand should be invoked when the hyperlink is clicked? Do I create a resource (as shown above), or it it done some other way? I need to do this in XAML--I don't want to resort to code-behind. Thanks for your help!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload