我型项目的一个观察的集合,我要显示在ListView,但没有被添加到我的ListView,我真的不明白
我MainWindow.xaml
< ListView控件名称=ListViewProjectsGrid.Column =0Grid.RowSpan =3的SelectionChanged =ListViewProjectsSelectionChanged的ItemsSource ={结合}IsSynchronizedWithCurrentItem =真了minWidth =100> < ListView.ItemTemplate> <的DataTemplate> < WrapPanel中> < TextBlock的文本={绑定路径= ProjectID}/> < TextBlock的文本={绑定路径=项目名}/> < / WrapPanel中> < / DataTemplate中> < /ListView.ItemTemplate> < / ListView控件>
我MainWindow.cs
公共部分类主窗口:窗口 { 的ObservableCollection<项目>项目=新的ObservableCollection<项目>(); 的ObservableCollection<员工>员工=新的ObservableCollection<员工>(); 公共主窗口() { 的InitializeComponent(); 的DataContext =项目; 项目PRO1 =新的项目(1,剑鱼); Projects.Add(PRO1); 员工empMads =新员工(的Mads,1); 员工empBrian =新员工(布莱恩,2); Employees.Add(empMads); Employees.Add(empBrian); } 私人无效ListViewProjectsSelectionChanged(对象发件人,SelectionChangedEventArgs E) { } }和我Project.cs它是类文件
[Serializable接口] 类项目:INotifyPropertyChanged的 { 公共项目(INT ID,字符串名称) { ID = ID; 名称=名称; } 私人诠释身份证; 公众诠释ID { {返回ID; } 组 { ID =价值; NotifyPropertyChanged(ProjectID); } } 私人字符串名称; 公共字符串名称 { {返回名称; } 组 { 名称=值; NotifyPropertyChanged(项目名); } } [现场:非序列化] 公共事件PropertyChangedEventHandler的PropertyChanged; 私人无效NotifyPropertyChanged(字符串信息) { 如果(的PropertyChanged!= NULL) { 的PropertyChanged(这一点,新PropertyChangedEventArgs(信息)); } } }
但没有被添加到我的清单我不能看到我缺少的是什么,它的工作。
我把它作为一个的ObservableCollection 我做的DataContext =集合 我在XAML文件不具有约束力
编辑codepart 1:
公开的ObservableCollection<项目>项目{获得;组; } 公众的ObservableCollection<员工>员工{获得;组; } 公共主窗口() { 的InitializeComponent(); 项目=新的ObservableCollection<项目>(); 员工=新的ObservableCollection<员工>(); 的DataContext =项目;解决方案
这是因为你使用像 ProjectID 绑定路径,而你的项目类有属性 ID 。这同样适用于项目名和名称属性。
E: 如果您有问题,结合它是非常有用的浏览输出在Visual Studio中的调试模式选项卡中看到来自数据绑定引擎返回了什么样的错误。这些异常通常不返回给用户,但你可以检查他们那里。
I have a observable collection of type Project, that I want to be displayed in a ListView but nothing is added to my ListView which I really dont understand
My MainWindow.xaml
<ListView Name="ListViewProjects" Grid.Column="0" Grid.RowSpan="3" SelectionChanged="ListViewProjectsSelectionChanged" ItemsSource="{Binding}" IsSynchronizedWithCurrentItem="True" MinWidth="100"> <ListView.ItemTemplate> <DataTemplate> <WrapPanel> <TextBlock Text="{Binding Path=ProjectID}"/> <TextBlock Text="{Binding Path=ProjectName}"/> </WrapPanel> </DataTemplate> </ListView.ItemTemplate> </ListView>My MainWindow.cs
public partial class MainWindow : Window { ObservableCollection<Project> Projects = new ObservableCollection<Project>(); ObservableCollection<Employee> Employees = new ObservableCollection<Employee>(); public MainWindow() { InitializeComponent(); DataContext = Projects; Project pro1 = new Project(1, "Swordfish"); Projects.Add(pro1); Employee empMads = new Employee("Mads", 1); Employee empBrian = new Employee("Brian", 2); Employees.Add(empMads); Employees.Add(empBrian); } private void ListViewProjectsSelectionChanged(object sender, SelectionChangedEventArgs e) { } }and my Project.cs which is the class file
[Serializable] class Project : INotifyPropertyChanged { public Project(int id, string name) { ID = id; Name = name; } private int id; public int ID { get { return id; } set { id = value; NotifyPropertyChanged("ProjectID"); } } private string name; public string Name { get { return name; } set { name = value; NotifyPropertyChanged("ProjectName"); } } [field: NonSerialized] public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged(String info) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(info)); } } }But nothing is added to my list I cant see what I am missing for it to work.
I have it as a observablecollection I do DataContext = the collection And I do binding in the xaml file
Edit codepart 1:
public ObservableCollection<Project> Projects { get; set; } public ObservableCollection<Employee> Employees { get; set; } public MainWindow() { InitializeComponent(); Projects = new ObservableCollection<Project>(); Employees = new ObservableCollection<Employee>(); DataContext = Projects;解决方案
It's because you use binding path like ProjectID while your Project class has property ID. The same goes for ProjectName and Name properties.
E: When you have problems with binding it's very useful to look through Output tab in visual studio's debug mode to see what errors were returned from databinding engine. Those exceptions are normally not returned to user, but you can inspect them there.
更多推荐
获取一个WPF列表视图显示的ObservableCollection&LT; T&GT;使用数据绑定
发布评论