2017-01-29 77 views
1

我正在學習WPF。先謝謝您的幫助。WPF:只能直接綁定到List,如果直接設置DataContext

我有一個對象,Directory,它充當一個List的Person對象的容器。我不明白爲什麼我不能將ListBox綁定到Person列表,除非我直接設置DataContext 。換句話說,我不能使用點符號來作爲目錄的子屬性訪問列表。

觀察下面的C#清晰代碼的最後一行:我將DataContext設置爲this.directory.People,它很好用。

但是,如果我將DataContext簡單設置爲this(以引用整個窗口),然後嘗試使用點符號來設置我的綁定像<ListBox ItemsSource="{Binding Path=directory.People}" />我的ListBox是空的。

下面列出了XAML。觀察XAML的最後一行。

代碼隱藏:

public class Person 
    { 
     public string Name { get; set; } 
     public int Age { get; set; } 
    } 

    public class Directory 
    { 
     public List<Person> People = new List<Person>(); 
     public Directory() 
     { 
      this.People.Add(new Person() { Name = "Joseph", Age = 34 }); 
      this.People.Add(new Person() { Name = "Teresa", Age = 29}); 
      this.People.Add(new Person() { Name = "Kulwant", Age = 66 }); 
      this.People.Add(new Person() { Name = "Hyunh", Age = 61}); 
      this.People.Add(new Person() { Name = "Marcio", Age = 65 }); 
     } 
    } 


    public partial class MainWindow : Window 
    { 

     public Directory directory { get; } = new Directory(); 

     public MainWindow() 
     { 
      InitializeComponent(); 
      this.DataContext = this.directory.People; 
     } 

    } 

XAML:

<Window x:Class="WtfDataTrigger.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:t="clr-namespace:System.Threading;assembly=mscorlib" 
     xmlns:local="clr-namespace:LearningWPF" 
     mc:Ignorable="d" 
     Title="MainWindow" Height="350" Width="525"> 

    <Window.Resources> 

     <DataTemplate DataType="{x:Type local:Person}"> 
      <Grid> 
       <Grid.RowDefinitions> 
        <RowDefinition Height="1*" /> 
        <RowDefinition Height="1*" /> 
       </Grid.RowDefinitions> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="50" /> 
        <ColumnDefinition Width="Auto" /> 
       </Grid.ColumnDefinitions> 
       <Label Name="NameLabel" Margin="2" Content="Name" Grid.Column="0" Grid.Row="0" VerticalAlignment="Center" FontWeight="Bold"/> 
       <TextBlock Name="NameText" Margin="2" Text="{Binding Path=Name}" Grid.Column="1" Grid.Row="0" VerticalAlignment="Center" /> 
       <Label Name="AgeLabel" Margin="2" Content="Age" Grid.Column="0" Grid.Row="1" VerticalAlignment="Center" FontWeight="Bold" /> 
       <TextBlock Name="AgeText" Margin="2" Text="{Binding Path=Age}" Grid.Column="1" Grid.Row="1" VerticalAlignment="Center"/> 
      </Grid> 
     </DataTemplate> 

    </Window.Resources> 

    <StackPanel> 
     <ListBox ItemsSource="{Binding}" /> 
    </StackPanel> 

</Window> 

回答

1

WPF數據綁定,只有公共屬性的作品。雖然directory是公共財產(但應命名爲Directory),People是一個公共領域。

變化

public List<Person> People = new List<Person>(); 

public List<Person> People { get; } = new List<Person>(); 
+0

謝謝。我已經知道了,我想我錯過了它。把我的頭髮撕掉了。乾杯。 – MH175