绑定标签为listView项目(C#,WPF)
-
13-09-2019 - |
题
我正在尝试将标签绑定到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帮助我如何实现这一目标?非常感谢!
干杯
解决方案
将集合结合到 ListView
(或任何 ItemsControl
, ,为此),您需要设置 ItemsSource
财产。这应该绑定到您的实例 AddressList
班级,假设收集是您想要在列表中显示的内容。
完成此操作后,您需要在 ListView
, ,类似于您在示例代码底部的评论如何描述它。
其他提示
This example binds to an XML data source, but you should be able to adapt it to your needs.
See also the MSDN documentation for ListView here.
不隶属于 StackOverflow