c# - 为什么 TreeView 控件不绑定(bind)

标签 c# wpf mvvm data-binding treeview

这是我的模型:

public class TreeItem : INotifyPropertyChanged
{
    private ObservableCollection<TreeItem> _items;
    private string _fullPath;
    private string _name;

    public string Name
    {
        get
        {
            return _name;
        }
        set
        {
            _name = value;
            OnPropertyChanged("Name");
        }
    }

    public string FullPath
    {
        get
        {
            return _fullPath;
        }
        set
        {
            _fullPath = value;
            OnPropertyChanged("FullPath");
        }
    }

    public ObservableCollection<TreeItem> Items
    {
        get
        {
            return _items;
        }
        set
        {
            _items = value;
            OnPropertyChanged("Items");
        }
    }

    public TreeItem(string name)
    {
        Name = name;
        Items = new ObservableCollection<TreeItem>();
    }

    public TreeItem()
    {
        Items = new ObservableCollection<TreeItem>();
    }


    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged([CallerMemberName]string prop = "")
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(prop));
    }
}

我实现了 INotifyPropertyChanged,因为我认为,通过添加这个实现可以解决这个问题。所以有我的 Xaml TreeView:
<TreeView Name="FileFolderTree" Grid.Column="2" Grid.Row="2" Background="DarkGray" ItemsSource="{Binding FolderFiles}">
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate DataType="{x:Type model:TreeItem}" ItemsSource="{Binding Path=Items}">
            <TextBlock Text="{Binding Name}" />
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

这是带有 DataContext 的 MainWindow.cs 等于新的 MainWindowViewModel:
public MainWindow()
{
    InitializeComponent();
    DataContext = new MainWindowViewModel();
}

所以, MainWindowViewModel 有一个公共(public)属性:
private  TreeItem _folderFiles;

public TreeItem FolderFiles {
    get
    {
        return _folderFiles;
    }
    set
    {
        _folderFiles = value;
        OnPropertyChanged("FolderFiles");
    }
}

在某些方法中,我看到 _folderFiles 包含数据,但它没有显示在 treeView 上。如果我将硬编码 treeView 中的某些元素,它们将被显示。

我做错了什么?

最佳答案

你的问题在这里:

public TreeItem FolderFiles 
{
    get
    {
        return _folderFiles;
    }
    set
    {
        _folderFiles = value;
        OnPropertyChanged("FolderFiles");
    }
}

此属性的类型为 TreeItem ,但您试图在 TreeView.ItemsSource 的绑定(bind)中使用它属性(property)。该属性需要 IEnumerable目的。如果您查看调试输出窗口,您会发现那里出现数据错误,因为绑定(bind)无法提供正确的值。

将您的属性(property)更改为 IEnumerable<TreeItem> .

创建 ObservableCollection<TreeItem>作为根容器:
public ObservableCollection<TreeItem> FolderFiles { get; } = new ObservableCollection<TreeItem>();

//...

FolderFiles.Add(_folderFiles);

如果您只想拥有一个根元素,您可以执行以下操作:
private TreeItem _folderFiles;
public IEnumerable<TreeItem> FolderFiles => new[] { _folderFiles };

(请注意,此示例不支持 INotifyPropertyChangedINotifyCollectionChanged ,但您肯定可以实现它。)

关于c# - 为什么 TreeView 控件不绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50969272/

相关文章:

c# - WPF,Caliburn.Micro和Dapper组合框

WPF:当 MVVM 绑定(bind)属性更改时激活触发器

c# - 为什么我无法使用 ef6 code First mysql 迁移来更新数据库?

c# - 如何将CSS应用于表格行

wpf - 如何使所有文本大写/大写?

WPF 按钮在窗口启动或激活期间具有键盘焦点(虚线边框)

c# - Silverlight ComboBox 绑定(bind)到 IEnumerable<BitmapImage>,其中图像从服务器下载

c# - 如何在 Xamarin.iOS 中向后滑动?

c# - 用于将中间用户输入从 ViewModel 传递到 Model 的 MVVM 模式

wpf - 您如何在 WPF 项目中结合直接 View-to-Model 方法和 MVVM 的优势?