[英]Binding not working when using listview with an expander in WPF
First of all I am new and have just started learning WPF. 首先,我是新手,刚刚开始学习WPF。
I have an employee class as shown below: 我有一个雇员班级,如下所示:
public class Employee : INotifyPropertyChanged
{
public Employee()
{
_subEmployee = new ObservableCollection<Employee>();
}
private string _name;
public ObservableCollection<Employee> SubEmployee
{
get { return _subEmployee; }
set
{
_subEmployee = value;
NotifiyPropertyChanged("SubEmployee");
}
}
public string Name
{
get { return _name; }
set
{
_name = value;
NotifiyPropertyChanged("Name");
}
}
ObservableCollection<Employee> _subEmployee;
public event PropertyChangedEventHandler PropertyChanged;
void NotifiyPropertyChanged(string property)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
I have created an observable collection of employee in MainWindow class and set the datacontext to the same collection as shown below: 我在MainWindow类中创建了一个可观察的雇员集合,并将datacontext设置为相同的集合,如下所示:
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 = Emp;
}
}
Now, in xaml.cs I have written following code: 现在,在xaml.cs中,我编写了以下代码:
<Grid>
<ListView ItemsSource="{Binding Emp}">
<ListView.ItemTemplate>
<DataTemplate DataType="{x:Type vm:Employee }">
<Expander Header="{Binding Name}">
<ListView ItemsSource="{Binding SubEmployee}">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text ="{Binding Name}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Expander>
</DataTemplate>
</ListView.ItemTemplate> >
</ListView>
</Grid>
Now I should get the collection of Employee object with an expander button, clicking on button should give me all the SubEmployee. 现在,我应该使用扩展器按钮来获取Employee对象的集合,单击该按钮应该给我所有的SubEmployee。 But unfortunately I am not getting the expected result.
但是不幸的是我没有得到预期的结果。
Please help me to resolve the binding problem. 请帮助我解决绑定问题。
Your DataContext
is ObservableCollection<Employee>
so outer LitView
should bind ItemsSource
directly to current DataContext
. 您的
DataContext
是ObservableCollection<Employee>
因此外部LitView
应该将ItemsSource
直接绑定到当前DataContext
。 In other words there is no Emp
property in your DataContext
. 换句话说,您的
DataContext
没有Emp
属性。 Your context is Emp
property 您的上下文是
Emp
属性
<ListView ItemsSource="{Binding}">
<ListView.ItemTemplate>
<!-- .... -->
</ListView.ItemTemplate>
</ListView>
or leave XAML as is and changed how you set DataContext
to 或保留XAML不变,并更改了将
DataContext
设置为
this.DataContext = this;
EDIT 编辑
Thought this was copy/paste error but since you complaint about an Invalid Operation Exception error it's because of second >
at the end of ItemTemplate
tag 以为这是复制/粘贴错误,但是由于您抱怨无效的操作异常错误,这是因为
ItemTemplate
标记末尾的第二个>
</ListView.ItemTemplate> >
I have tested your code. 我已经测试过您的代码。 The
DataContext
was not set properly. DataContext
设置不正确。 I also gave a color to the text. 我还给文本加了颜色。 Try this.
尝试这个。 Working perfectly at my end!
在我的末端完美工作!
XAML : XAML:
<Grid>
<ListView ItemsSource="{Binding Emp}">
<ListView.ItemTemplate>
<DataTemplate DataType="{x:Type vm:Employee }">
<Expander Header="{Binding Name}">
<ListView ItemsSource="{Binding SubEmployee}">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Foreground="Black" Text ="{Binding Name}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Expander>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
Code Behind : 背后的代码:
public partial class MainWindow : Window
{
public ObservableCollection<Employee> Emp { get; set; }
public MainWindow()
{
InitializeComponent();
DataContext = this;
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" });
}
}
The other data class remains the same! 其他数据类保持不变!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.