[英]Binding not working properly in treeview WPF
I have an Employee
Class as shown below: 我有一个
Employee
Class,如下所示:
public class Employee : INotifyPropertyChanged
{
public Employee()
{
_subEmployee = new ObservableCollection<Employee>();
}
public string Name { get; set; }
public ObservableCollection<Employee> SubEmployee
{
get { return _subEmployee; }
set
{
_subEmployee = value;
NotifiyPropertyChanged("SubEmployee");
}
}
ObservableCollection<Employee> _subEmployee;
public event PropertyChangedEventHandler PropertyChanged;
void NotifiyPropertyChanged(string property)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
I am creating a collection of employee class in Main window constructor and adding it to an observable collection of employee as shown below: 我正在Main窗口构造函数中创建一个employee类的集合,并将其添加到一个可观察的员工集合中,如下所示:
public partial class MainWindow : Window
{
public ObservableCollection<Employee> Emp { get; private set; }
public MainWindow()
{
InitializeComponent();
Emp = new ObservableCollection<Employee>();
Emp.Add(new Employee(){Name = "Anuj"});
Emp.Add(new Employee() { Name = "Deepak" });
Emp.Add(new Employee() { Name = "Aarti" });
Emp[0].SubEmployee.Add(new Employee(){Name = "Tonu"});
Emp[0].SubEmployee.Add(new Employee() { Name = "Monu" });
Emp[0].SubEmployee.Add(new Employee() { Name = "Sonu" });
Emp[2].SubEmployee.Add(new Employee() { Name = "Harsh" });
Emp[2].SubEmployee.Add(new Employee() { Name = "Rahul" });
Emp[2].SubEmployee.Add(new Employee() { Name = "Sachin" });
this.DataContext = this;
}
}
I have set the DataContext as self. 我已将DataContext设置为self。 Now, in xaml file I have created a hierarchical template of treeview and binded data as shown below:
现在,在xaml文件中,我创建了树视图和绑定数据的分层模板,如下所示:
<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TreeView ItemsSource="{Binding}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding SubEmployee}">
<TextBlock Text="{Binding Name}" />
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
</Grid>
</Window>
Now when I keep, TreeView ItemsSource="{Binding Emp}"
, binding works properly and I can see the tree view structure after running the code. 现在,当我保留
TreeView ItemsSource="{Binding Emp}"
,绑定正常工作,我可以在运行代码后看到树视图结构。 However when I keep TreeView ItemsSource="{Binding}"
, I see no result after running the code. 但是,当我保留
TreeView ItemsSource="{Binding}"
,运行代码后看不到任何结果。
To my understanding, keeping ItemSource = "{Binding}"
means I am binding to the evaluated value of the current datacontext. 据我所知,保持
ItemSource = "{Binding}"
意味着我绑定到当前datacontext的评估值。 As my datacontext is set to self, ItemSource = "{Binding}"
should mean I am binding to the only property of DataContext ie Emp and I should get proper result. 当我的datacontext设置为self时,
ItemSource = "{Binding}"
应该意味着我绑定到DataContext的唯一属性,即Emp和我应该得到正确的结果。
Please help me in understanding the problem I am getting in keeping binding as ItemSource = "{Binding}"
. 请帮助我理解我在保持绑定方面遇到的问题,如
ItemSource = "{Binding}"
。
"To my understanding, keeping
ItemSource = "{Binding}"
means I am binding to the evaluated value of the current datacontext."“根据我的理解,保持
ItemSource = "{Binding}"
意味着我绑定到当前datacontext的评估值。”
Correct AND that is the issue. 正确而且这就是问题所在。 ItemsSource expects the binding source to be of type
IEnumerable
but you are binding to Window
. ItemsSource期望绑定源的类型为
IEnumerable
但是您绑定到Window
。
"...should mean I am binding to the only property of DataContext ie Emp and I should get proper result."
“...应该意味着我绑定到DataContext的唯一属性,即Emp和我应该得到正确的结果。”
No. No such "single property" assumption exists in WPFs binding conventions. 不可以。在WPF绑定约定中不存在这样的“单一属性”假设。
Change... 更改...
this.DataContext = this;
To... 至...
this.DataContext = Emp;
Or, alternatively, change binding in XAML and specify the correct member on the DataContext to bind to using Path
... 或者,或者,在XAML中更改绑定并在DataContext上指定要绑定到使用
Path
的正确成员...
<TreeView ItemsSource="{Binding Path=Emp}">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.