Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将项目绑定到wpf treeview时不显示任何内容?_C#_Wpf_Xaml - Fatal编程技术网

C# 将项目绑定到wpf treeview时不显示任何内容?

C# 将项目绑定到wpf treeview时不显示任何内容?,c#,wpf,xaml,C#,Wpf,Xaml,当我在xaml中设置了itemsource时,为什么我的wpf树视图不显示内容?它仅在我使用以下命令在CS文件中设置此属性时显示数据 trvFamilies.ItemsSource = families; 是不是因为我没有在xaml中触发和更新而没有更新?我不确定要改变什么才能让它正常工作 ViewModel.cs MainWindow.xaml 当前,您正在绑定到公共字段: public List<Family> families 最后,您需要在窗口中将新的视图模型类设置为

当我在xaml中设置了itemsource时,为什么我的wpf树视图不显示内容?它仅在我使用以下命令在CS文件中设置此属性时显示数据

trvFamilies.ItemsSource = families;
是不是因为我没有在xaml中触发和更新而没有更新?我不确定要改变什么才能让它正常工作

ViewModel.cs

MainWindow.xaml


当前,您正在绑定到公共字段:

  public List<Family> families

最后,您需要在窗口中将新的视图模型类设置为DataContext,请参见。

您是否可以修改我的代码以演示其外观?我始终建议ObservableCollection私下声明并实例化为只读,并且没有公共setter,因此,人们只能清除列表或添加项,实际上不需要RaisePropertyChanged调用。在这种情况下,我更改了字符串的错误,复制粘贴错误:-是的,可以将族的setter设置为私有。我倾向于在绑定到的属性中始终实现INotifyPropertyChanged,只是为了避免潜在的错误。更重要的是保护自己不受人们可能绑定到CollectionChanged事件的影响,如果不观察ViewModel中的属性族是否发生更改,他们还必须保留对它的引用,以便从旧的ObservableCollection中注销。但是RaisePropertyChanged调用在当时也是不必要的,因此它确实简化了事情。顺便说一句,你的私人家庭在这里被宣布为公共的;所以我更新了上面的帖子,它似乎还没有显示出来?为什么会这样。查看我的最新代码您现在必须绑定到您的ViewModel,而不是您的窗口。当然,请查看调试输出,它可能会说,族本身已不存在
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));
            }
        }
    }
}
<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
public ObservableCollection<Family> Families { get; set; }
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}"