简体   繁体   English

为什么WPF XAML DataContext绑定不起作用?

[英]Why WPF XAML DataContext binding doesn't work?

I cannot understand why my DataContext binding doesn't work when I run my app. 我无法理解为什么运行我的应用程序时我的DataContext绑定不起作用。 I am also using design-time datacontext and it works. 我也使用设计时datacontext,它的工作原理。

Here are the main parts from my XAML. 以下是我的XAML的主要部分。 This is from MainWindow.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">

This is also from MainWindow.xaml 这也来自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>

This is the main TreeView. 这是主要的TreeView。 I need to display everything here. 我需要在这里展示一切。

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

And here is C# code. 这是C#代码。 This is from MainWindow.xaml.cs, namespace is Logs_Cleaner_WPF 这是来自MainWindow.xaml.cs,名称空间是Logs_Cleaner_WPF

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

DisplayItem: DisplayItem:

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));
    }
}

Because of the declaration before, your DataContext is already Items collection! 由于之前的声明,您的DataContext已经是Items集合!

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

So binding to ItemsSource should just be {Binding } 所以绑定到ItemsSource应该只是{Binding }

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

In your case it's {Binding Items} again, so it's trying to bind to Items.Items , which does not exist. 在你的情况下,它再次是{Binding Items} ,所以它试图绑定到不存在的Items.Items

I think it is better to rewrite it in an appropriate way, as @NETscape suggested. 我认为最好以适当的方式重写它,就像@NETscape建议的那样。 I'm going to implement MVVM. 我要实现MVVM。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM