C# 为什么WPF XAML DataContext绑定没有';不行?
我无法理解为什么我的DataContext绑定在运行应用程序时不起作用。我还使用了设计时datacontext,它可以正常工作 下面是我的XAML的主要部分。这是来自MainWindow.xaml的C# 为什么WPF XAML DataContext绑定没有';不行?,c#,wpf,xaml,C#,Wpf,Xaml,我无法理解为什么我的DataContext绑定在运行应用程序时不起作用。我还使用了设计时datacontext,它可以正常工作 下面是我的XAML的主要部分。这是来自MainWindow.xaml的 <Window x:Class="Logs_Cleaner_WPF.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.mic
<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">
<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>
由于前面的声明,您的
DataContext
已经是Items
集合
DataContext="{Binding Items, RelativeSource={RelativeSource Self}}"
所以绑定到ItemsSource
应该是{binding}
<TreeView ItemsSource="{Binding }" x:Name="TreeView" ... />
在你的例子中,它又是
{Binding Items}
,所以它试图绑定到Items.Items
,它并不存在。我认为最好按照@NETscape的建议,以适当的方式重写它。我将实现MVVM。如何用数据填充Items集合?已加载私有void(对象发送方,RoutedEventTargets RoutedEventTargets){{u dirHelper=App.UnityContainer.Resolve();JustForTest();}私有void JustForTest(){Items=new observeCollection();Items.Add(new DisplayFolder{Checked=true,Path=“MyPath7”});}您可能会发现通读起来很有用:)请不要将代码放在注释中,而是编辑您的问题并将代码包含在其中。我更改了TreeView ItemsSource=“{Binding}”但这仍然是同一个问题。design dataContext可以工作,但如果我运行该程序,它不会显示任何内容,尽管Items集合不是空的。@Maksym这是正确的答案,但应该注意的是,如果您这样做,您还必须更改设计时数据结构或其绑定。另一种方法是将更改Window.DataContext
绑定以省去“Items”,因此它将DataContext设置为Window对象本身,而不将其设置为Window.Items
属性。DataContext=“{binding RelativeSource={RelativeSource Self}”
@Rachel正确的答案可能是遵循MVVM并为集合创建一个实际的视图模型,然后适当地设置Window的DataContext,并可能正确地使用HierarchicalDataTemplates。我为您遵循MVVM的决定鼓掌,但这并不是真正的答案。找出哪些答案是正确的osted为您解决了最初提出的问题,根据进行标记,并删除此(您的)答案;或删除此问题的全部内容。我们很乐意帮助在本课程中学习MVVM的任何人
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));
}
}
DataContext="{Binding Items, RelativeSource={RelativeSource Self}}"
<TreeView ItemsSource="{Binding }" x:Name="TreeView" ... />