[英]Binding to Window.DataContext.ViewModelCommand inside a ItemsControl
我有一些像这样的窗口:
<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
绑定不起作用。 我猜这个问题是因为我的按钮在 ItemsControl 内,所以绑定正在寻找FooType
对象内的ViewModelCommand
。
那么我怎样才能正确地进行这种绑定呢?
该DataTemplate
中的DataContext
将是FooType
项; 这就是ItemTemplate
的用途:显示每个项目。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.