将哈希集绑定到ListView项(C#,WPF)

将哈希集绑定到ListView项(C#,WPF),c#,wpf,inotifypropertychanged,hashset,C#,Wpf,Inotifypropertychanged,Hashset,我正在尝试将哈希集绑定到ListView项。我在这里记录了我的代码: public class Person { public string Name { get; set; } public AddressList = new AddressList (); } public class AddressList : HashSet<Addresses> { // } public class Addresses { public string Str

我正在尝试将哈希集绑定到ListView项。我在这里记录了我的代码:

public class Person {
    public string Name { get; set; }
    public AddressList = new AddressList ();
}
public class AddressList : HashSet<Addresses>
{
    //
}
public class Addresses {
    public string Streetname { get; set; }
    public string City { get; set; }
}
public class PersonViewModel : INotifyPropertyChanged {
    private Person _person;

    public PersonViewModel(Person person)
    {
        _person= person; 
    }

    public string Name
    {
        get { return _person.Name; }
        set
        {
            _person.Name = value;
            OnPropertyChanged("Name");
        }
    }
    private void OnPropertyChanged(string property)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }
}

 // This is how I add the DataContext: mainGrid.DataContext = _person //this is a PersonViewModel();
 // This is how I bind the DataObjects to the GUI Elements: <TextBox Name="TxtBoxName" Text="{Binding Path=.Name, Mode=TwoWay}"/>
 // How can I add in the same way a HashSet to a ListView Element in the WPF Gui? I tried something like {Binding Path=.Name, Mode=TwoWay}
公共类人物{
公共字符串名称{get;set;}
公共地址列表=新地址列表();
}
公共类地址列表:HashSet
{
//
}
公共类地址{
公共字符串Streetname{get;set;}
公共字符串City{get;set;}
}
公共类PersonViewModel:INotifyPropertyChanged{
私人(私人),;
公共PersonViewModel(个人)
{
_人=人;
}
公共字符串名
{
获取{return\u person.Name;}
设置
{
_person.Name=值;
不动产变更(“名称”);
}
}
私有void OnPropertyChanged(字符串属性)
{
if(PropertyChanged!=null)
{
PropertyChanged(此,新PropertyChangedEventArgs(property));
}
}
}
//这就是我添加DataContext的方式:mainGrid.DataContext=\u person//这是一个PersonViewModel();
//这就是我将DataObjects绑定到GUI元素的方式:
//如何以相同的方式向WPF Gui中的ListView元素添加哈希集?我尝试了类似{Binding Path=.Name,Mode=TwoWay}
有人能帮我学习tipps吗?如何实现这一点?非常感谢


Cheers

要将集合绑定到
列表视图
(或任何
项控件
),您需要设置其
项资源
属性。这应该绑定到
AddressList
类的实例,假设集合是您希望在列表中显示的集合

完成此操作后,您需要为
列表视图中的每一列设置绑定,类似于示例代码底部的注释对其的描述。

绑定到XML数据源,但您应该能够根据需要调整它


另请参阅ListView的MSDN文档。

另外,除非您为Addresses类添加(或需要)IEqualityComparer,否则我将避免使用HashSet而不是更简单的集合,比如List。但是HashSet不是更快吗?实际上,我不需要任何特定的HashSet函数(除了“nicethave”惟一条目部分)。。