【问题标题】:BindingExpression path error when binding to a check command of a Checkbox绑定到 Checkbox 的检查命令时出现 BindingExpression 路径错误
【发布时间】:2020-03-15 02:26:31
【问题描述】:

我有一个带有一列复选框的DataGrid,并且我在DataGrid 标头中有一个复选框,选中时会选中所有复选框。基于this answer,我有一个绑定到“checked”事件的命令和另一个绑定到“unchecked”事件的命令。

所有相关文件都在下面(当然是简化的)

我的 XAML:

<DataGridTemplateColumn Width="40">
    <DataGridTemplateColumn.Header>
        <CheckBox>
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="Checked">
                    <i:InvokeCommandAction Command="{Binding CheckAllRowsCommand}"/>
                </i:EventTrigger>
                <i:EventTrigger EventName="Unchecked">
                    <i:InvokeCommandAction Command="{Binding UncheckAllRowsCommand}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </CheckBox>
    </DataGridTemplateColumn.Header>

    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <CheckBox IsChecked="{Binding Selected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>

</DataGridTemplateColumn>

我的 xaml.cs

public partial class MyTableView: UserControl
{
    public MyTableView()
    {
        InitializeComponent();
        DataContext = new MyTableViewModel();
    }
}

MyTableViewModel.cs

public class MyTableViewModel: BaseViewModel
{
    public MyTableViewModel() : base()
    {
        CheckAllRowsCommand= new CheckAllRowsCommand(this);
        UncheckAllRowsCommand = new UncheckAllRowsCommand(this);
    }

    public ICommand CheckAllRowsCommand{ get; }
    public ICommand UncheckAllRowsCommand{ get; }
}

CheckAllRowsCommand

public class CheckAllRowsCommand: BaseCommand
{
    public CheckAllRowsCommand(MyTableViewModel parent) : base(parent)
    {
    }

    public override bool CanExecute(object parameter)
    {
        return true;
    }

    public override void Execute(object parameter)
    {
       // Set the Selected property of each data row
    }
}

运行时出现以下错误:

System.Windows.Data 错误:40:BindingExpression 路径错误: 在“对象”“CheckBox”上找不到“CheckAllRowsCommand”属性 (名称='')'。绑定表达式:路径=CheckAllRowsCommand; DataItem='CheckBox' (Name='');目标元素是“InvokeCommandAction” (哈希码=47015983);目标属性是“命令”(输入“ICommand”)

任何帮助将不胜感激。

【问题讨论】:

    标签: c# wpf xaml data-binding


    【解决方案1】:

    CheckAllRowsCommand 是 ViewModel 中的属性,而不是 CheckBox 中的属性。尝试通过 DataGrid 绑定到 viewModel(它应该继承了 DataContext):

    <DataGrid Name="NameOfDataGrid" ...>
    
    <i:InvokeCommandAction Command="{Binding DataContext.CheckAllRowsCommand, ElementName=NameOfDataGrid}"/>
    

    【讨论】:

      【解决方案2】:

      在我看来,这是一个比 Ash 发布的更冗长但更灵活的解决方案。

      <CheckBox DataContext="{Binding Path=DataContext, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}">
      ...
      </CheckBox>
      

      问题是模板中的绑定似乎使用CheckBox 作为它的数据上下文,而您认为它会使用包含控件的数据上下文。 WPF 有时可能很时髦,任何时候我看到这样的错误(即BindingExpression path error: 'blah' property not found on 'object' ''blah')我总是认为数据上下文没有被正确推断。

      我提出的解决方法是使用相对源强制在CheckBox 上设置数据上下文。例如,相对源可用于声明性地说“使用此类型的第一个父级”。这个绑定是说“将我的 DataContext 属性设置为我上面第一个父级的DataContext DataGrid”。从逻辑上讲,DataGrid 类型的复选框的第一个父级将是您想要的那个。然后你的内部命令绑定不需要改变,一切都应该按预期工作。

      【讨论】:

        【解决方案3】:

        您可以将CheckBoxDataContext 绑定到父UserControlDataContext,其中命令属性使用{RelativeSource} 定义:

        <CheckBox DataContext="{Binding DataContext, RelativeSource={RelativeSource AncestorType=UserControl}}">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="Checked">
                    <i:InvokeCommandAction Command="{Binding CheckAllRowsCommand}"/>
                </i:EventTrigger>
                <i:EventTrigger EventName="Unchecked">
                    <i:InvokeCommandAction Command="{Binding UncheckAllRowsCommand}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </CheckBox>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-04-22
          • 1970-01-01
          • 2015-05-21
          • 1970-01-01
          • 2013-05-14
          • 1970-01-01
          • 1970-01-01
          • 2021-02-13
          相关资源
          最近更新 更多