我有一个对象集合和ViewModel中的一个命令。
我想为集合中的每个对象显示一个超链接,并将每个超链接的命令设置为相同的命令,将对象ID作为CommandParameter传递给该命令。例如:
// View Model
public class MyViewModel : ViewModelBase
{
// Raises PropertyChanged event, ommited here
public List<MyClass> MyList {....}
public RelayCommand<int> MyCommand {....}
}
我将UserControl的DataContext设置为上述ViewModel类。此UserControl的XAML如下:
<UserControl>
<ItemsControl ItemsSource="{Binding Path=MyList}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<HyperlinkButton Content="{Binding Path=Description}" Command="{Binding Path=MyCommand}" CommandParameter="{Binding Path=MyClassID}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</UserControl>
超链接内容的描述显示正确,但命令从未触发,我猜测这是因为它正在寻找MyClass对象内的命令?如何绑定到UserControl的DataContext.MyCommand而不是它正在寻找的MyClass.MyCommand?