将ListView选择的项目绑定到DataGrid [英] Bind ListView selected item to a DataGrid

查看:182
本文介绍了将ListView选择的项目绑定到DataGrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ListView,它包含一个名字列表,每个名字都来自一个包含name字段的对象的绑定,如下所示:

  public class User {
public User(){}
public User(string name,int ID){
this.name = name;
this.ID = ID;
}
public string name {set; get;}
public int ID {set; get;}
}

和listView:

 < ListView Margin =10Name =lvNameDisplayMemberPath =name/> 

并且在后面的代码中,我将lvName.ItemSource设置为用户对象的ObservableCollection,在集合I拥有相同名称但不相同ID的用户。



我还有一个dataGrid,其中定义了2列(一个用于名称,一个用于ID)我想要这样做,以便当用户从列表视图(显示用户名)中选择一行时,数据网格将填充所有用户对象(名称和ID),名称与所选的名称相同列表视图。



我该怎么做?



注意:我设法将名称字段绑定到文本框,方法如下:

  Text ={Binding SelectedItem.name,ElementName = lvName}

我不知道如何在数据网格上执行此操作,更不用说ID字段以及名称字段。

解决方案

这可以使用MVVM方法轻松完成。
假设用户类看起来像这样:

  public class User 
{
public int Id {get;组; }
public string Name {get;组;
}

您可以定义此视图模型:

  public class ViewModel 
{
public ViewModel()
{
Users = new ObservableCollection< User>
{
new User {Id = 1,Name =John},
new User {Id = 2,Name =Mary},
new User { 3,Name =Peter},
new User {Id = 4,Name =John},
new User {Id = 5,Name =John},
new User {Id = 5,Name =Peter}
};

UsersView = new ListCollectionView(Users)
{
Filter = obj =>
{
var user =(User)obj;

return SelectedUser!= null&& user.Name == SelectedUser.Name&&& user.Id!= selectedUser.Id;
}
};
}

public ObservableCollection< User>用户{get;私人集合}
public ICollectionView UsersView {get;组; }

public User SelectedUser
{
get {return selectedUser;
set
{
if(selectedUser!= value)
{
selectedUser = value;
UsersView.Refresh();
}
}
}
私人用户selectedUser;
}

XAML:



< p $ p> < StackPanel>
< ListView DisplayMemberPath =NameItemsSource ={Binding Users}SelectedItem ={Binding SelectedUser}/>
< DataGrid ItemsSource ={Binding UsersView}/>
< / StackPanel>

结果:




I have a ListView that holds a list of names, each name comes from a binding to an object containing a "name" field, like this:

public class User {
    public User() {}
    public User(string name, int ID) {
        this.name = name;
        this.ID = ID;
    }
    public string name {set; get;}
    public int ID {set; get;}
 }

and the listView:

    <ListView Margin="10" Name="lvName" DisplayMemberPath="name"/>

and in the code behind I set lvName.ItemSource to an ObservableCollection of User objects, in the collection I have Users that can have the same Name, but not the same ID.

I also have a dataGrid, with 2 columns defined(one for name and one for ID), I want to make it so that when the user selects a row from the listview(which shows names of Users), the datagrid would be populated with all the User objects(both the name and ID) that have the same name as was selected in the listview.

how can I do that?

Note: I managed to bind the name field to a textblock, by doing this:

 Text="{Binding SelectedItem.name, ElementName=lvName}"

but I have no idea how to do it on a datagrid, let alone with the ID field as well as the name field.

解决方案

This could be easily done using MVVM approach. Assuming, that User class looks like this:

public class User
{
    public int Id { get; set; }
    public string Name { get; set; }
}

you can define this view model:

public class ViewModel
{
    public ViewModel()
    {
        Users = new ObservableCollection<User>
        {
            new User { Id = 1, Name = "John" },
            new User { Id = 2, Name = "Mary" },
            new User { Id = 3, Name = "Peter" },
            new User { Id = 4, Name = "John" },
            new User { Id = 5, Name = "John" },
            new User { Id = 5, Name = "Peter" }
        };

        UsersView = new ListCollectionView(Users)
        {
            Filter = obj =>
            {
                var user = (User)obj;

                return SelectedUser != null && user.Name == SelectedUser.Name && user.Id != selectedUser.Id;
            }
        };
    }

    public ObservableCollection<User> Users { get; private set;  }
    public ICollectionView UsersView { get; set; }

    public User SelectedUser
    {
        get { return selectedUser; }
        set
        {
            if (selectedUser != value)
            {
                selectedUser = value;
                UsersView.Refresh();
            }
        }
    }
    private User selectedUser;
}

XAML:

<StackPanel>
    <ListView DisplayMemberPath="Name" ItemsSource="{Binding Users}" SelectedItem="{Binding SelectedUser}"/>
    <DataGrid ItemsSource="{Binding UsersView}"/>
</StackPanel>

Result:

这篇关于将ListView选择的项目绑定到DataGrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆