【问题标题】:BindingExpression Path error in View Model Commands视图模型命令中的 BindingExpression 路径错误
【发布时间】:2013-09-23 20:23:47
【问题描述】:

我的视图模型中的命令绑定到我的 WPF 应用程序中的用户控件时遇到问题。这些命令在用户选中或取消选中 checkBox 时运行。话虽如此,这些命令显然绑定到checkBoxes

运行程序后,我的输出窗口对每个命令都有以下错误(请注意,当checkBoxes 被选中或取消选中时,这些命令在运行期间不起作用):

System.Windows.Data Error: 40 : BindingExpression path error: 'MyCommand' property not found on 'object' 'ViewModel' (HashCode=3383440)'. BindingExpression:Path=MyCommand; DataItem='ViewModel' (HashCode=3383440); target element is 'CheckBox' (Name='checkBox1'); target property is 'Command' (type 'ICommand')

这就是我的 XAML 的样子:

<CheckBox Content="CheckBox" Command="{Binding MyCommand}" .../>

我的视图模型中的 C# 代码:

private Command _myCommand;

public Command MyCommand { get { return _myCommand; } }
private void MyCommand_C()
{
    //This is an example of how my commands interact with my model
    _dataModel._groupBoxEnabled = true;
}

内部构造函数:

_myCommand = new Command(MyCommand_C);

【问题讨论】:

    标签: c# wpf mvvm command


    【解决方案1】:

    您需要将视图模型分配给视图的 DataContext。您的 *.xaml.cs 中有什么代码?应该是这样的:

    public MyView( )
    {
        this.DataContext = new ViewModel( );
    }
    

    将来,您可以告诉您的视图模型没有被输出中的信息连接:

    System.Windows.Data 错误:40:BindingExpression 路径错误: 在“object”“ViewModel”上找不到“MyCommand”属性 (哈希码=3383440)'。绑定表达式:路径=我的命令; DataItem='ViewModel' (HashCode=3383440);目标元素是“复选框” (名称='checkBox1');目标属性是“命令”(输入“ICommand”)

    它所说的对象是DataContext,如果它显示为'object'类型,它不是'ViewModel'类型,这意味着你还没有将它分配给DataContext .

    回答 cmets 中关于与数据交互的问题:

    命令允许您进一步将逻辑与 UI 分开,这很棒。但在某些时候,您可能想从 ViewModel 与 UI 对话。为此,您需要使用可以notify the UI of when they are changed 的属性。因此,在您的命令中,您可以在 CheckBox 的 Checked 属性绑定到的 ViewModel 上设置一个属性(比如 IsChecked)。所以你的 Xaml 看起来像:

    <CheckBox Content="CheckBox" Checked="{Binding IsChecked}" Command="{Binding MyCommand}" .../>
    

    您的 ViewModel 可能如下所示:

    private Command _myCommand;
    private bool _isChecked;
    
    public Command MyCommand { get { return _myCommand; } }
    public bool IsChecked
    {
        /* look at the article to see how to use getters and setters */
    }
    
    private void MyCommand_C()
    {
        IsChecked = !IsChecked;
        _dataModel._groupBoxEnabled = IsChecked;
    }
    

    如果您想在已经是视图模型属性的对象上包装属性,只需使用(我称之为)包装器属性。

    public bool IsChecked
    {
        get
        {
            return _dataModel.MyCheckBox;
        }
        set
        {
            if(_dataModel != null)
            {
                _dataModel.MyCheckBox = value;
                OnPropertyChanged("IsChecked");
            }
            // Exception handling if _dataModel is null
        }
    }
    

    【讨论】:

    • 感谢您的关注。我也有一个数据模型。这些命令与我的数据模型中的数据交互,因此它仍然显示 BindingExpression 路径错误。我需要包含哪些内容才能与该模型正确交互?
    • 什么意思?也许您可以在问题中解释更多和/或发布更多代码,因为这会有所帮助。
    • 我在命令方法中添加了一些内容。如您所见,我调用了数据模型中的属性。
    • 如果我的CheckBox 控件的IsChecked 属性设置为{Binding DataModel.MyCheckBox} 怎么办?
    • 现在得跑了。如果这有帮助,请不要忘记标记为答案并投票!祝你好运!
    猜你喜欢
    • 2013-05-14
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-11
    • 2020-03-15
    相关资源
    最近更新 更多