欢迎光临!
我在我的项目(带有C#的Silverlight3)中遇到了这个问题:
我有一个TreeView,它将数据绑定到树上。此TreeView在资源字典中有一个HierarchicalDataTamplate,它定义了各种控件。现在我想隐藏(Visibility.Collapse)一些依赖于节点是否有子节点的项。其他项目应在相同条件下可见。
当我第一次将源树绑定到TreeView时,它的工作方式类似于charm,但当我更改源树时,树视图中的可见性不变。
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,可以让用户输入在这个叶节点中生成的“流”。(即: Node.Throughput = Node.Flow + Sum(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。
如果有人能向我解释一下,为什么在底层树中插入项不会更新box和block的可见性,我将不胜感激。
提前谢谢你!
发布于 2009-09-18 09:41:09
发生的情况是,只要属性发生更改,Converter就会被调用。
但是,将项添加到集合中并不构成更改属性。毕竟,它仍然是同一个集合。您需要做的是在集合发生变化时将ViewModel转换为NotifyPropertyChanged。这将导致转换器重新计算集合。
https://stackoverflow.com/questions/1443237
复制相似问题