gpt4 book ai didi

ListBoxItem MVVM 命令绑定(bind)中的 WPF ListBox 命令

转载 作者:行者123 更新时间:2023-12-03 10:58:15 25 4
gpt4 key购买 nike

我一直在尝试找到一种在我的 中添加命令按钮的方法。 ListBoxItem 的 列表框 控制。我用 MVVM 和命令通过 ICommand 接口(interface)绑定(bind)到 DataContext .我无法找到从 ListBox ItemsSource 中绑定(bind)到 Views DataContext 的方法。我继续得到 命令“未找到”错误 .谁能帮我理解如何做到这一点。我相信它与命令绑定(bind)路径有关,但我不确定。任何帮助或方向都会很棒。

最佳答案

DataContext ListBox 中的项目数将是他们所代表的项目。因此,如果您的 ItemsSource绑定(bind)到一个小部件列表,然后 DataContext您的ListBoxItem将是一个小部件。

如果您需要访问DataContextListBox ,而不是 ListBoxItem , 那么你可以使用 RelativeSource :

<ListBox.ItemTemplate>
<DataTemplate>
<Button Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListBox}},
Path=DataContext.MyCommand}" />

关于ListBoxItem MVVM 命令绑定(bind)中的 WPF ListBox 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31007687/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com