将HashSet绑定到ListView项(C#,WPF)

时间:2009-06-15 15:25:19

标签: c# wpf inotifypropertychanged hashset

我正在尝试将HashSet绑定到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}

任何人都可以帮助我tipps如何实现这一目标?非常感谢!

干杯

2 个答案:

答案 0 :(得分:1)

要将集合绑定到ListView(或任何ItemsControl),您需要设置其ItemsSource属性。这应绑定到AddressList类的实例,假设该集合是您希望在列表中显示的内容。

完成后,您需要为ListView中的每一列设置绑定,类似于示例代码底部的注释对其进行描述。

答案 1 :(得分:0)

This example绑定到XML数据源,但您应该能够根据自己的需要进行调整。

另请参阅ListView here的MSDN文档。