无法在 ListBox 内绑定命令 [英] Cannot bind command within a ListBox

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

问题描述

我的 WPF 使用 MVVM 方法.我正在尝试在我的列表控件中绑定 2 个控件

My WPF uses the MVVM approach. I'm trying to bind 2 controls within my list control

<ListBox ItemsSource="{Binding ParentDuplicate}" SelectedItem="{Binding SelectedParent, UpdateSourceTrigger=PropertyChanged}">
    <ListBox.ItemTemplate>
         <DataTemplate>
             <StackPanel>
                    <ContentControl Content="{Binding}" />
                    <Button Content="Delete me now" 
                            Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorLevel=1, AncestorType=Window}, Path=DeleteCommand}" 
                            CommandParameter="{Binding FilePath}" />
              </StackPanel>
           </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

我遇到的问题是 DeleteCommand 没有绑定(输出窗口也会通知我)

The problem I'm having, is the DeleteCommand is not binding (the Ouput window informs me as well)

System.Windows.Data 错误:4:无法找到引用RelativeSource FindAncestor, AncestorType='System.Windows.Data.Binding', AncestorLevel='1''的绑定源.BindingExpression:Path=DeleteCommand;数据项=空;目标元素是 'Button' (Name='');目标属性是'Command'(类型'ICommand')

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Data.Binding', AncestorLevel='1''. BindingExpression:Path=DeleteCommand; DataItem=null; target element is 'Button' (Name=''); target property is 'Command' (type 'ICommand')

如果我将此按钮移到 ListBox 之外,则绑定会起作用并且事件会触发,因此我知道问题一定出在 ListBox 上(我猜问题是 ItemsSource 阻止绑定到除 ItemsSource 绑定之外的任何内容)属性(在这种情况下,ParentDuplicate))

If I move this button to outside the ListBox, then the binding works and the event fires so I know the issue must be with the ListBox (I'm guessing the problem is the ItemsSource prevents the binding to anything but the ItemsSource bound property (in this case, ParentDuplicate))

因此,在 Button 控件中,绑定了 2 个属性,DeleteCommandFilePath

So, within the Button control, there are 2 properties being bound, DeleteCommand and FilePath

这两个属性都存在于我的单个 ViewModel 中.FilePath 是 ParentDuplicate 的子项,它可以根据需要进行绑定.问题仅在于 DeleteCommand.我做错了什么?

Both of these properties live within my single ViewModel. FilePath is a child of ParentDuplicate and this binds as desired. The issue is only with the DeleteCommand. What am I doing wrong?

编辑

当我使用 Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorLevel=1, AncestorType=Window}, Path=DeleteCommand} 时,它正在查看我的 MainWindow 代码,而不是在 ViewModel 中.如何让它使用 ViewModel?

When I use the Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorLevel=1, AncestorType=Window}, Path=DeleteCommand} it is looking in my MainWindow code behind, not at the ViewModel. How do I make it use the ViewModel?

我试过了

Command="{Binding RelativeSource={RelativeSource  AncestorType=xmlnsViewModel:MainWindowViewModel}, Path=DeleteCommand}" 

但以上也导致相同的绑定错误

but the above also results in the same binding errors

推荐答案

祖先搜索找到的是控件,而不是 DataContext,因此您需要告诉绑定在哪里可以找到 DeleteCommand 属性.如果您的 ViewModel 是 MainWindow 的 DataContext 那么您可以使用:

The ancestor search finds the control, not the DataContext, so you'll need to tell your binding where to find the DeleteCommand property. If your ViewModel is the DataContext of the MainWindow then you can just use:

<Button Content="Delete me now" 
    Command="{Binding RelativeSource={RelativeSource 
                  Mode=FindAncestor, AncestorLevel=1, AncestorType=Window}, 
                  Path=DataContext.DeleteCommand}" 
    CommandParameter="{Binding FilePath}" />

这篇关于无法在 ListBox 内绑定命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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