我跑進我的項目(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的。
如果有人能向我解釋,爲什麼插入項目到底層樹不會更新框和塊的可見性,我將不勝感激。
預先感謝您!