ListBoxItem MVVM 命令绑定中的 WPF ListBox 命令 [英] WPF ListBox Commands within ListBoxItem MVVM Command Binding
问题描述
我一直在尝试找到一种方法,在 ListBox 控件的 ListBoxItem's 中包含命令按钮.我通过 ICommand 接口使用 MVVM 和命令绑定到 DataContext.我无法找到从 ListBox ItemsSource 中绑定到 Views DataContext 的方法.我继续收到命令未找到"错误.谁能帮我理解如何做到这一点.我相信它与命令绑定路径有关,但我不确定.任何帮助或指导都会很棒.
I have been trying to find a way to have command buttons within my ListBoxItem's of a ListBox control. I use MVVM and Command Binding to the DataContext via ICommand interface. I am unable to find a way to bind to the Views DataContext from within the ListBox ItemsSource. I continue to get a Command "Not Found" error. Can anyone help me understand how to accomplish this. I believe that it has something to do with the command binding path, but I am unsure. Any help or direction would be great.
推荐答案
ListBox
中项目的DataContext
将是它们所代表的项目.因此,如果您的 ItemsSource
绑定到一个 Widget 列表,那么您的 ListBoxItem
的 DataContext
将是一个 Widget.
The DataContext
of items in a ListBox
will be the item they represent. So if your ItemsSource
is bound to a list of Widgets, then the DataContext
of your ListBoxItem
will be a Widget.
如果需要访问ListBox
的DataContext
,而不是ListBoxItem
,那么可以使用RelativeSource代码>:
If you need to access the DataContext
of the ListBox
, rather than the ListBoxItem
, then you can use RelativeSource
:
<ListBox.ItemTemplate>
<DataTemplate>
<Button Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListBox}},
Path=DataContext.MyCommand}" />
这篇关于ListBoxItem MVVM 命令绑定中的 WPF ListBox 命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!