ListBoxItem MVVM 命令绑定中的 WPF ListBox 命令 [英] WPF ListBox Commands within ListBoxItem MVVM Command Binding

查看:53
本文介绍了ListBoxItem MVVM 命令绑定中的 WPF ListBox 命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试找到一种方法,在 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 列表,那么您的 ListBoxItemDataContext 将是一个 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.

如果需要访问ListBoxDataContext,而不是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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆