0

我正在尝试将 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}

任何人都可以帮助我提示如何做到这一点吗?非常感谢!

干杯

4

2 回答 2

1

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

完成此操作后,您需要为 中的每一列设置绑定ListView,类似于示例代码底部的注释描述它的方式。

于 2009-06-15T16:49:48.837 回答
0

此示例绑定到 XML 数据源,但您应该能够根据需要对其进行调整。

另请参阅此处的 ListView 的 MSDN 文档。

于 2009-06-15T17:15:46.680 回答