2009-09-18 50 views
1

問候!樹視圖 - 分層數據模板 - 綁定不會更新源更改?

我跑進我的項目(Silverlight 3的用C#)這個問題:

我有一個TreeView這是必然,那麼,一棵樹的數據。 此TreeView在資源字典中有一個HierarchicalDataTamplate,它定義了各種控件。現在我想隱藏(Visibility.Collapse)一些項目取決於節點是否有子節點。其他項目應在相同條件下可見。

當我第一次將源代碼樹綁定到TreeView時,它的作用就像魅力一樣,但是當我更改源代碼樹時,樹視圖中的可見性不會改變。

XAML - 頁:

<controls:TreeView x:Name="SankeyTreeView" 
    ItemContainerStyle="{StaticResource expandedTreeViewItemStyle}" 
    ItemTemplate="{StaticResource SankeyTreeTemplate}"> 
    <controls:TreeViewItem IsExpanded="True"> 
     <controls:TreeViewItem.HeaderTemplate> 
      <DataTemplate> 
       <TextBlock Text="This is just for loading and will be replaced directly after the data becomes available..."/> 
      </DataTemplate> 
     </controls:TreeViewItem.HeaderTemplate> 
    </controls:TreeViewItem> 
</controls:TreeView> 

XAML - ResourceDictionary中

<!-- Each node in the tree is structurally identical, hence only one Hierarchical 
    Data Template that'll use itself on the children. --> 
<Data:HierarchicalDataTemplate x:Key="SankeyTreeTemplate" 
    ItemsSource="{Binding Children}"> 
    <Grid Height="24"> 
    <TextBlock x:Name="TextBlockName" Text="{Binding Path=Value.name, Mode=TwoWay}" 
     VerticalAlignment="Center" Foreground="Black"/> 
    <TextBox x:Name="TextBoxFlow" Text="{Binding Path=Value.flow, Mode=TwoWay}" 
     Grid.Column="1" Visibility="{Binding Children, 
     Converter={StaticResource BoxConverter}, 
     ConverterParameter=\{box\}}"/> 
    <TextBlock x:Name="TextBlockThroughput" Text="{Binding Path=Value.throughput, Mode=TwoWay}" 
     Grid.Column="1" Visibility="{Binding Children, 
     Converter={StaticResource BoxConverter}, ConverterParameter=\{block\}}"/> 
    <Button x:Name="ButtonAddNode"/> 
    <Button x:Name="ButtonDeleteNode"/> 
    <Button x:Name="ButtonEditNode"/> 
    </Grid> 
</Data:HierarchicalDataTemplate> 

現在,你可以看到,在TextBoxFlow和TextBlockThroughput共享相同的空間。 我的目標是:節點的「吞吐量」值是從它的子節點通過此節點「流動」多少東西。它不能直接更改,所以我想顯示一個文本塊。只有葉節點有一個TextBox來讓別人輸入在這個葉節點中生成的'流'。 (IE:Node.Throughput = Node.Flow +薩姆(Children.Throughput),其中Node.Flow = 0對於每個非葉)

什麼BoxConverter(傻名稱-.-)的作用:

public object Convert(object value, Type targetType, object parameter, 
    System.Globalization.CultureInfo culture) 
{ 
    if ((value as NodeList<TreeItem>).Count > 1) // Node has Children? 
    { 
     if ((parameter as String) == "{box}") 
     { 
      return Visibility.Collapsed; 
     } 
     else ((parameter as String) == "{block}") 
     { 
      return Visibility.Visible; 
     } 
    } 
    else 
    { 
     /* 
     * As above, just with Collapsed and Visible switched 
     */ 
    } 
} 

綁定到TreeView的樹的結構實質上是從Dan Vanderboom(這裏有點太多來轉儲整個代碼)偷走的結構,除了我在這裏當然使用ObservableCollection作爲子項和值項INotifyPropertyChanged的。

如果有人能向我解釋,爲什麼插入項目到底層樹不會更新框和塊的可見性,我將不勝感激。

預先感謝您!

+0

此綁定「Children」是ObservableCollection的子類,因此應該提供自動收集更新通知。 – Cornelius 2009-09-21 07:55:45

回答

0

發生的情況是,只要屬性發生更改,就會調用Converter。

但是,將項目添加到集合中並不構成更改屬性。畢竟它仍然是同一個集合。當集合更改時,您需要做的是將ViewModel設置爲NotifyPropertyChanged。這將導致轉換器重新評估集合。