Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将DataGrid的MVVM SelectedItem设置为CommandParameter并接收时显示为null_C#_Wpf_Mvvm_Data Binding_Datagrid - Fatal编程技术网

C# 将DataGrid的MVVM SelectedItem设置为CommandParameter并接收时显示为null

C# 将DataGrid的MVVM SelectedItem设置为CommandParameter并接收时显示为null,c#,wpf,mvvm,data-binding,datagrid,C#,Wpf,Mvvm,Data Binding,Datagrid,问题 我试图将CommandParameter设置为GridName.SelectedItem,但在我的paramater命令中,它显示为null值 错误代码-执行按钮时 An unhandled exception of type 'System.NullReferenceException' occurred in TestingOnly.exe Additional information: Object reference not set to an instance of an obj

问题

我试图将CommandParameter设置为GridName.SelectedItem,但在我的paramater命令中,它显示为null值

错误代码-执行按钮时

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
    }
}
我在这里发现了其他类似的问题,其中提到了在视图中设置
模式=TwoWay
,但这对我来说没有任何改变

问题

An unhandled exception of type 'System.NullReferenceException' occurred in TestingOnly.exe
Additional information: Object reference not set to an instance of an object.
我错在哪里?如何修复此空值错误

目标

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);
    }
}

我只是想从datagrid中获取所选的
ID
Name
,并在
命令
类中使用它。

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

应该是:

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

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

应该是:

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

啊,我知道你在那里做了什么。消息框显示为
TestingOnly.Models.BasicTestModel
。如何使用参数从命令类访问
ID
Name
?按类型castin object parameter查看模型类型:
var vm=parameter as BasicTestModel;MessageBox.Show(vm.name)public FSQMUpdateRecordCommand(FSQM\u RecordViewModel viewModel)
)绑定是更糟糕的做法(即使它没有违反mvvm)。查找一些ICommand实现:RelayCommand、DelegateCommand。它们是可重用的,并且具有通用版本,不需要参数类型转换。感谢您提供的信息,我将对它们进行一些研究。您提到的命令。啊,我知道您在那里做了什么。消息框显示为
TestingOnly.Models.BasicTestModel
。如何使用参数从命令类访问
ID
Name
?按类型castin object parameter查看模型类型:
var vm=parameter as BasicTestModel;MessageBox.Show(vm.name)public FSQMUpdateRecordCommand(FSQM\u RecordViewModel viewModel)
)绑定是更糟糕的做法(即使它没有违反mvvm)。查找一些ICommand实现:RelayCommand、DelegateCommand。它们是可重用的,并且具有通用版本,不需要参数类型转换。感谢您提供的信息,我将对它们进行一些研究。