2016-01-06 86 views
3

我不明白爲什麼我的DataContext綁定不起作用,當我運行我的應用程序。我也使用設計時數據上下文,它的工作原理。爲什麼WPF XAML 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; } 
} 

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

如何使用數據填充Items集合? –

+0

private void OnLoaded(object sender,RoutedEventArgs routedEventArgs) { _dirHelper = App.UnityContainer.Resolve (); JustForTest(); } private void JustForTest() { Items = new ObservableCollection (); Items.Add(new DisplayFolder {Checked = true,Path =「MyPath7」}); } – Maksym

+0

你可能會發現[這個關於DataContext的回答](http://stackoverflow.com/a/7262322/302677)對於通讀有用:) – Rachel

回答

2

因爲在報關前,你的DataContext已經Items收藏!

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

所以要ItemsSource結合應該只是{Binding }

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

在你的情況下,它{Binding Items}了,所以它試圖綁定到Items.Items,它不存在。

+0

我改變了TreeView ItemsSource =「{Binding}」,但它仍然是同樣的問題。設計dataContext的作品,但如果我運行該程序 - 它不顯示任何東西,雖然Items集合不是空的。 – Maksym

+1

@Maksym這是正確的答案,但應該注意的是,如果你走這條路線,你還必須改變你的設計時間數據結構或綁定。另一種方法是改變'Window.DataContext'綁定以省略「Items」,因此它將DataContext設置爲Window對象本身,並且不會將其設置爲'Window.Items'屬性。 'DataContext =「{Binding RelativeSource = {RelativeSource Self}}」' – Rachel

+0

@Rachel正確的答案可能是遵循MVVM併爲集合創建一個實際的視圖模型,然後適當地設置Window的DataContext,並且可能正確地使用HierarchicalDataTemplates 。 – Kcvin

-2

我認爲最好是用@NETscape建議的適當方式重寫它。我要實現MVVM。

+2

我爲你決定遵循MVVM而鼓掌,但這不是真正的答案。找出已發佈的哪些答案針對您提出的原始問題,並根據標記並刪除此(您的)答案;或者全部刪除這個問題。 – Kcvin

+1

我們很樂意幫助任何人在[WPF聊天室]中學習MVVM(http://chat.stackoverflow.com/rooms/18165/wpf) – Kcvin