我有一些窗口,例如:
<Window>
<ItemsControl ItemsSource="{Binding MyItemList}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Command="{Binding ViewModelCommand}">My Button</Button>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Window>
此窗口具有使用以下实例定义的 DataContext
属性:
public class MyVM
{
public IEnumerable<FooType> MyItemList { get; set; }
public ICommand ViewModelCommand { get; set; }
}
问题是 Button.Command
绑定(bind)不起作用。我猜测问题是因为我的按钮位于 ItemsControl 内部,因此绑定(bind)正在 FooType
对象内部查找 ViewModelCommand
。
那么我怎样才能正确绑定(bind)呢?
最佳答案
DataTemplate
内的 DataContext
将是 FooType
项;这就是 ItemTemplate
的用途:显示每个项目。
关于c# - 绑定(bind)到 ItemsControl 内的 Window.DataContext.ViewModelCommand,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37040712/