2009-09-18 56 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。这将导致转换器重新评估集合。