gpt4 book ai didi

c# - 绑定(bind)在 treeview WPF 中无法正常工作

转载 作者:行者123 更新时间:2023-11-30 14:49:57 25 4
gpt4 key购买 nike

我有一个 Employee 类,如下所示:

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));
}
}

我正在主窗口构造函数中创建员工类的集合并将其添加到可观察的员工集合中,如下所示:

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;
}
}

我已将 DataContext 设置为 self。现在,在 xaml 文件中,我创建了 TreeView 和绑定(bind)数据的分层模板,如下所示:

<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>

现在,当我保留 TreeView ItemsSource="{Binding Emp}" 时,绑定(bind)工作正常运行代码后我可以看到 TreeView 结构。但是,当我保留 TreeView ItemsSource="{Binding}" 时,运行代码后看不到任何结果。

据我了解,保持 ItemSource = "{Binding}" 意味着我绑定(bind)到当前数据上下文的评估值。由于我的 datacontext 设置为 self,ItemSource = "{Binding}" 应该意味着我绑定(bind)到 DataContext 的唯一属性,即 Emp,我应该得到正确的结果。

请帮助我理解我在保持绑定(bind)方面遇到的问题ItemSource = "{Binding}"

最佳答案

"To my understanding, keeping ItemSource = "{Binding}" means I am binding to the evaluated value of the current datacontext."

正确,这就是问题所在。 ItemsSource 期望绑定(bind)源的类型为 IEnumerable 但您要绑定(bind)到 Window

"...should mean I am binding to the only property of DataContext i.e. Emp and I should get proper result."

没有。 WPF 绑定(bind)约定中不存在此类“单一属性”假设。

改变...

this.DataContext = this;

为了...

this.DataContext = Emp;

或者,或者,更改 XAML 中的绑定(bind)并在 DataContext 上指定正确的成员以使用 Path...

绑定(bind)
<TreeView ItemsSource="{Binding Path=Emp}">

关于c# - 绑定(bind)在 treeview WPF 中无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38032182/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com