【问题标题】:MVVM SelectedItem of DataGrid shows as null when setting it as CommandParameter and recieving it将 DataGrid 的 MVVM SelectedItem 设置为 CommandParameter 并接收时显示为 null
【发布时间】:2021-04-23 15:03:00
【问题描述】:

问题

我正在尝试将 CommandParameter 设置为命令的 GridName.SelectedItem,但在我的参数命令中,它显示为空值。

错误代码 - 执行按钮时

An unhandled exception of type 'System.NullReferenceException' occurred in TestingOnly.exe
Additional information: Object reference not set to an instance of an object.

完整代码

查看

<DataGrid x:Name="FSQMGrid" 
          ItemsSource="{Binding TestList}"
          AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding ID}" Header="Id"/>
        <DataGridTextColumn Binding="{Binding Name}" Header="Name"/>
    </DataGrid.Columns>
</DataGrid>

<StackPanel Grid.Column="1">
    <Button Content="Update" 
            Command="{Binding FSQMUpdateRecordCommand}" 
            CommandParameter="{Binding FSQMGrid.SelectedItem}"/>
</StackPanel>

型号

public class BasicTestModel
{
    public int ID { get; set; }
    public string Name { get; set; }
}

查看模型

public class FSQM_RecordViewModel : BaseViewModel
{

    private List<BasicTestModel> _TestList { get; set; }

    public List<BasicTestModel> TestList { get { return _TestList; } set { _TestList = value; OnPropertyChanged("TestList"); } }

    public ICommand FSQMUpdateRecordCommand { get; set; }

    public FSQM_RecordViewModel()
    {
        TestList = new List<BasicTestModel>
        {
            new BasicTestModel { ID = 1, Name = "Name 1"},
            new BasicTestModel { ID = 2, Name = "Name 2"},
            new BasicTestModel { ID = 3, Name = "Name 3"}
        };

        FSQMUpdateRecordCommand = new FSQMUpdateRecordCommand(this);
    }
}

命令

public class FSQMUpdateRecordCommand : ICommand
{
    public FSQM_RecordViewModel viewModel { get; set; }

    public FSQMUpdateRecordCommand(FSQM_RecordViewModel viewModel)
    {
        this.viewModel = viewModel;
    }

    public event EventHandler CanExecuteChanged;

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

    public void Execute(object parameter)
    {
        System.Windows.MessageBox.Show(parameter.ToString()); // Shows as null
    }
}

我的尝试

我在这里发现了其他类似的问题,提到在视图中设置Mode=TwoWay,但这对我来说并没有改变任何东西。

问题

我哪里错了?如何解决这个空值错误?

目标

我的目标只是从数据网格中获取选定的IDName 并在Command 类中使用它。

【问题讨论】:

    标签: c# wpf mvvm data-binding datagrid


    【解决方案1】:

    无法解析与路径 FSQMGrid.SelectedItem 的绑定。 DataContext 中没有 FSQMGrid 属性。 FSQMGrid 是 UI 元素的名称

    应该是:

    CommandParameter="{Binding Path=SelectedItem, ElementName=FSQMGrid}"
    

    【讨论】:

    • 啊,我明白你在那里做了什么。 MessageBox 显示为TestingOnly.Models.BasicTestModel。如何使用参数从 Command 类访问 IDName
    • 通过 type-castin 对象参数查看模型类型:var vm = parameter as BasicTestModel; MessageBox.Show(vm.name);
    • 在应用 MVVM 模式方面这是一个好习惯吗?
    • 为什么不呢?为每个操作创建命令类型,绑定到某些视图模型类型(如public FSQMUpdateRecordCommand(FSQM_RecordViewModel viewModel))是更糟糕的做法,IMO(即使它不违反 mvvm)。找到一些 ICommand 实现:RelayCommand、DelegateCommand。它们是可重用的并且具有通用版本,不需要对参数进行类型转换
    • 感谢您提供的信息,我将对您提到的命令进行一些研究。
    猜你喜欢
    • 1970-01-01
    • 2011-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-30
    • 1970-01-01
    • 2019-06-18
    • 1970-01-01
    相关资源
    最近更新 更多