为什么WPF XAML DataContext绑定没有';不起作用

本文关键字:不起作用 WPF XAML DataContext 绑定 为什么 | 更新日期: 2023-09-27 18:29:07

我不明白为什么我的DataContext绑定在运行应用程序时不起作用。我也在使用设计时数据上下文,它是有效的。

以下是XAML的主要部分。这是来自MainWindow.xaml

<Window x:Class="Logs_Cleaner_WPF.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="600" Width="800" MinWidth="800" MinHeight="600" WindowStartupLocation="CenterScreen"
    xmlns:data="clr-namespace:Logs_Cleaner_WPF.Data"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    DataContext="{Binding Items, RelativeSource={RelativeSource Self}}"
    d:DataContext="{Binding Source={d:DesignInstance Type=data:DesignData, IsDesignTimeCreatable=True}}"
    mc:Ignorable="d">

这也是来自MainWindow.xaml

<Window.Resources>
    <HierarchicalDataTemplate x:Key="ChildDataTemplate">
        <TextBlock Text="{Binding Path}"></TextBlock>
    </HierarchicalDataTemplate>
    <HierarchicalDataTemplate x:Key="RootDataTemplate" ItemsSource="{Binding DisplayFolders}" ItemTemplate="{StaticResource ChildDataTemplate}">
        <TextBlock Text="{Binding Path}"></TextBlock>
    </HierarchicalDataTemplate>
</Window.Resources>

这是主TreeView。我需要在这里展示所有东西。

<TreeView DataContext="{Binding}" ItemsSource="{Binding Items}"     x:Name="TreeView" Grid.Row="1" VerticalContentAlignment="Top" Margin="10,5" ItemTemplate="{StaticResource RootDataTemplate}">
    </TreeView>

这是C#代码。这来自MainWindow.xaml.cs,命名空间为Logs_Cleaner_WPF

public partial class MainWindow : Window
{
    public static ObservableCollection<DisplayItem> Items { get; set; }
}

显示项目:

public abstract class DisplayItem : INotifyPropertyChanged
{
    private string _path;
    public virtual string Path
    {
        get { return _path; }
        set
        {
            _path = value;
            OnPropertyChanged();
        }
    }
    private long _size;
    public long Size {
        get { return _size; }
        set
        {
            _size = value;
            OnPropertyChanged();
        } 
    }
    private bool _checked;
    public bool Checked
    {
        get { return _checked; }
        set
        {
            _checked = value;
            OnPropertyChanged();
        }
    }
    public override string ToString()
    {
        return Path;
    }
    public event PropertyChangedEventHandler PropertyChanged;
    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        var handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

为什么WPF XAML DataContext绑定没有';不起作用

由于之前的声明,您的DataContext已经是Items集合了!

DataContext="{Binding Items, RelativeSource={RelativeSource Self}}"

所以与ItemsSource的结合应该只是{Binding }

<TreeView ItemsSource="{Binding }" x:Name="TreeView" ... />

在您的情况下,它又是{Binding Items},所以它试图绑定到不存在的Items.Items

我认为最好按照@NETscape的建议,以适当的方式重写它。我要实现MVVM。