将项绑定到WPF树视图不会显示任何内容
本文关键字:显示 任何内 视图 绑定 WPF | 更新日期: 2023-09-27 18:18:00
为什么当我在xaml中设置了itemsource时,我的wpf树视图不显示内容?只有当我在CS文件中使用
设置此属性时,才会显示数据trvFamilies.ItemsSource = families;
它可能不更新,因为我没有触发和更新在某种程度上在xaml?我不知道该怎么做才能让它正常工作。
ViewModel.cs
using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Input;
namespace WpfApplication1
{
public class ViewModel : ObservableObject
{
private ObservableCollection<Family> families;
public ObservableCollection<Family> Families
{
get { return families; }
set
{
families = value;
NotifyPropertyChanged("Families");
}
}
public ViewModel()
{
// FAMILIES
Family family1 = new Family() { Name = "The Doe's" };
family1.Members.Add(new FamilyMember() { Name = "John Doe", Age = 42 });
family1.Members.Add(new FamilyMember() { Name = "Jane Doe", Age = 39 });
family1.Members.Add(new FamilyMember() { Name = "Sammy Doe", Age = 13 });
Families.Add(family1);
Family family2 = new Family() { Name = "The Moe's" };
family2.Members.Add(new FamilyMember() { Name = "Mark Moe", Age = 31 });
family2.Members.Add(new FamilyMember() { Name = "Norma Moe", Age = 28 });
Families.Add(family2);
}
}
public class Family
{
public Family()
{
this.Members = new ObservableCollection<FamilyMember>();
}
public string Name { get; set; }
public ObservableCollection<FamilyMember> Members { get; set; }
}
public class FamilyMember
{
public string Name { get; set; }
public int Age { get; set; }
}
}
ObservableObject.cs
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace WpfApplication1
{
public class ObservableObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
MainWindow.xaml
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:self="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="300"
WindowStartupLocation="CenterScreen">
<Grid Margin="5">
<TreeView Name="trvFamilies" ItemsSource="{Binding self:families}" Grid.Row="1" Grid.ColumnSpan="2">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type self:Family}" ItemsSource="{Binding Members}">
<StackPanel Orientation="Horizontal">
<Label VerticalAlignment="Center" FontFamily="WingDings" Content="1"/>
<TextBlock Text="{Binding Name}" />
</StackPanel>
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type self:FamilyMember}">
<StackPanel Orientation="Horizontal">
<Label VerticalAlignment="Center" FontFamily="WingDings" Content="2"/>
<TextBlock Text="{Binding Name}" />
</StackPanel>
</DataTemplate>
</TreeView.Resources>
</TreeView>
</Grid>
</Window>
当前绑定到一个公共字段:
public List<Family> families
但是绑定源(数据)需要是一个公共属性。对于集合类型,在WPF中通常使用ObservableCollection
所以把你的属性改成:
public ObservableCollection<Family> Families { get; set; }
附加信息:
通常你绑定到的属性不驻留在视图类中(这里:不在你的窗口中),而是在一个叫做"视图模型"的单独的类中,它应该实现INotifyPropertyChanged接口。
查看此处获取更多信息:Model-View-ViewModel-MVVM-Explained
最后,您的视图模型应该是这样的:public ObservableCollection<Family> Families
{
get { return _families; }
set
{
_families= value;
RaisePropertyChanged("Families");
}
}
public ObservableCollection<Family> _families;
protected void RaisePropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
新的视图模型类就位后,绑定可以更改为
ItemsSource="{Binding Families}"
最后你需要设置你的新视图模型类为DataContext在你的窗口,见这里