Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.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
.net 在XAML MVVM中绑定datagrid特定列的selectedvalue_.net_Wpf_Xaml - Fatal编程技术网

.net 在XAML MVVM中绑定datagrid特定列的selectedvalue

.net 在XAML MVVM中绑定datagrid特定列的selectedvalue,.net,wpf,xaml,.net,Wpf,Xaml,我在这个问题上坚持了很长时间,希望能得到一些帮助 因此,我有一个数据网格,其中填充了数据。 在我的viewmodel类中,我有一个Person属性,其属性为-> 名字、姓氏、出生日期、身份证等 现在,用户一次只能在datagrid中选择一行。对于所选行,我希望每个列都绑定到person对象的属性。在XAML中有什么方法可以做到这一点吗 非常感谢您可以通过使用DataGrid的SelectedItem属性来完成此操作,该属性将DataGrid所选项目直接绑定到其相应的ViewModel类属性。请参

我在这个问题上坚持了很长时间,希望能得到一些帮助

因此,我有一个数据网格,其中填充了数据。 在我的viewmodel类中,我有一个Person属性,其属性为->

名字、姓氏、出生日期、身份证等

现在,用户一次只能在datagrid中选择一行。对于所选行,我希望每个列都绑定到person对象的属性。在XAML中有什么方法可以做到这一点吗


非常感谢

您可以通过使用DataGrid的SelectedItem属性来完成此操作,该属性将DataGrid所选项目直接绑定到其相应的ViewModel类属性。请参阅下面的代码片段,如果有用,请标记答案

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = new ViewModel();
    }
}

//VIEW MODEL
public class ViewModel
{
    private Person _selectedPerson;
    public Person SelectedPerson
    {
        get { return _selectedPerson; }
        set { _selectedPerson = value; }
    }
    public List<Person> PersonCollection { get; set; }

    public ViewModel()
    {
        PersonCollection = new List<Person>();
        for (int i = 0; i < 10; i++)
        {
            PersonCollection.Add(new Person()
            {
                Id = i.ToString(),
                FirstName = "First Name " + i.ToString(),
                LastName = "Last Name " + i.ToString()
            });
        }
    }
}

//PERSON CLASS
public class Person
{
    public string Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

In XAML
-------

<Window x:Class="WpfApplication2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DataGrid ItemsSource="{Binding PersonCollection}"
                  SelectedItem="{Binding SelectedPerson}" />
    </Grid>
 </Window>
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
this.DataContext=newviewmodel();
}
}
//视图模型
公共类视图模型
{
私人(u selected Person);;
公众人士
{
获取{return\u selectedPerson;}
设置{u selectedPerson=value;}
}
公共列表PersonCollection{get;set;}
公共视图模型()
{
PersonCollection=新列表();
对于(int i=0;i<10;i++)
{
PersonCollection.Add(新人员()
{
Id=i.ToString(),
FirstName=“First Name”+i.ToString(),
LastName=“Last Name”+i.ToString()
});
}
}
}
//人类
公共阶层人士
{
公共字符串Id{get;set;}
公共字符串名{get;set;}
公共字符串LastName{get;set;}
}
在XAML中
-------

DataGrid绑定到的数据格式是什么?如果要绑定到某种类型的Person对象列表,那么很容易在DataGrid中将每个Person对象(实际上只是一个PersonViewModel)显示为一行,不同的列显示每个Person的属性。如果你详细介绍一下你所做的尝试(也许是邮政编码),我相信有人可以帮你。