2012-11-26 124 views
2

所以我有什麼應該是一個簡單的XAML數據綁定錯誤。我有兩個類(到目前爲止)的下面的XAML,他們用於DataBinding,一個行,其中包含一個ObservableCollection行。節點有一堆額外的相關信息,我試圖以類似網格的方式顯示這些節點(它將用於尋路算法)。WPF數據綁定不顯示

問題在於「Here」 TextBlock不顯示。但是我知道節點正在被正確綁定,因爲它們的值顯示在Debugging StackPanel中。

<Window.Resources> 
    <local:Row x:Key="TestRow"> 
     <local:Node x="0" y="0" Cost="20" /> 
     <local:Node x="0" y="1" Cost="20" /> 
     <local:Node x="0" y="2" Cost="20" /> 
    </local:Row> 
</Window.Resources> 
<Grid Name="MainGrid" Margin="10,10,10,10" > 
    <Grid.RowDefinitions> 
     <RowDefinition Height="150" /> 
     <RowDefinition Height="*" /> 
    </Grid.RowDefinitions> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="100" /> 
     <ColumnDefinition /> 
    </Grid.ColumnDefinitions> 
    <StackPanel Margin="0,25,0,0" 
       DataContext="{Binding ElementName=AStarListView, 
             Path=SelectedItem}" 
       x:Name="Debugging" Orientation="Vertical" > 
     <TextBlock Text="{Binding x}" /> 
     <TextBlock Text="{Binding y}" /> 
     <TextBlock Text="{Binding Cost}" /> 
    </StackPanel> 
    <ListView Grid.RowSpan="2" Margin="2,2,2,2" Grid.Column="1" 
       x:Name="AStarListView" 
       ItemsSource="{StaticResource TestRow}" > 
     <ListView.ItemTemplate> 
      <DataTemplate DataType="local:Row"> 
       <ListView ItemsSource="{Binding nodes}" Width="48" Height="48" > 
        <ListView.ItemTemplate> 
         <DataTemplate DataType="local:Node"> 
          <TextBlock Text="Here" /> 
         </DataTemplate> 
        </ListView.ItemTemplate> 
       </ListView> 
      </DataTemplate> 
     </ListView.ItemTemplate> 
    </ListView> 

這是(剝離的)節點類。

public class Node : INotifyPropertyChanged 
{ 
    public Tuple<int, int> coordinates { get; set; } 

    public int x 
    { 
     get 
     { 
      if (this.coordinates == null) 
       return -1; 
      return this.coordinates.Item1; 
     } 
     set 
     { 
      if (this.coordinates != null) 
       this.coordinates = new Tuple<int, int>(value, y); 
      else 
       this.coordinates = new Tuple<int, int>(value, -1); 

      OnPropertyChanged("x"); 
     } 
    } 
    public int y 
    { 
     get 
     { 
      if (this.coordinates == null) 
       return -1; 
      return this.coordinates.Item2; 
     } 
     set 
     { 
      if (this.coordinates != null) 
       this.coordinates = new Tuple<int, int>(x, value); 
      else 
       this.coordinates = new Tuple<int, int>(-1, value); 
      OnPropertyChanged("y"); 
     } 
    } 

    private Node _parent; 

    private int _Cost; 
    public int Cost 
    { 
     get 
     { 
      return _Cost; 
     } 
     set 
     { 
      _Cost = value; 
      OnPropertyChanged("Cost"); 
     } 
    } 

    protected void OnPropertyChanged(string propertyName) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChangedEventArgs args = 
       new PropertyChangedEventArgs(propertyName); 
      this.PropertyChanged(this, args); 
     } 
    } 
    public event PropertyChangedEventHandler PropertyChanged; 
} 

這裏的行類:

public class Row : ObservableCollection<Node> 
{ 
    public ObservableCollection<Node> nodes { get; set; } 

    public Row() 
    { 
     this.nodes = new ObservableCollection<Node>(); 
    } 
} 
+0

什麼是嵌套的ListView的目的?看起來像ItemsSource =「{綁定節點}」部分是沒有意義的。 –

+0

嵌套列表視圖將顯示行的集合。所以你有一個行的集合,它又有一個節點的集合。 –

+0

你可以在這裏發佈'Row'類的代碼嗎? –

回答

1

這裏的修正XAML,類定義是正確的問題,需要這方面的XAML取代「AStarListView」。

<ListView Grid.RowSpan="2" Margin="2,2,2,2" Grid.Column="1" x:Name="AStarListView" 
      ItemsSource="{StaticResource TestRow}" > 

    <ListView.ItemTemplate> 
     <DataTemplate DataType="local:Node"> 
      <Grid Background="#dddddd" > 
       <TextBlock Grid.Row="0" Grid.Column="0" Text="{Binding x}"/> 
       <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding y}"/> 
       <TextBlock Grid.Row="1" Grid.Column="0" Text="{Binding Cost}"/> 
       <Grid.RowDefinitions> 
        <RowDefinition Height="24"/> 
        <RowDefinition Height="24"/> 
       </Grid.RowDefinitions> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="24"/> 
        <ColumnDefinition Width="24"/> 
       </Grid.ColumnDefinitions> 
      </Grid> 
     </DataTemplate> 
    </ListView.ItemTemplate> 
</ListView> 

我的問題是我有ListViews太深嵌套。內部的ListView綁定到一個Node的「nodes」屬性,它不存在。