我试图通过IValueConverter使隐式生成的TreeViewItems崩溃。我删除了网格的代码,但网格存在于TreeViewItem中。如何使用IValueConverter使TreeViewItem崩溃?
<TreeView Name="TreeViewNew"
Background="Transparent"
HorizontalContentAlignment="Stretch"
VirtualizingStackPanel.IsVirtualizing="True"
ScrollViewer.VerticalScrollBarVisibility="Disabled"
>
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Path=MapLayers}">
<Grid>
<TextBlock
Focusable="False"
Width="150"
Text="{Binding Path=Name}"
FontSize="10"
TextWrapping="Wrap"
Background="Transparent"
HorizontalAlignment="Center"
/>
</Grid>
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate >
<Grid Visibility="{Binding Path=Layer, Converter={StaticResource cIsDrawingLayer}}">
答案 0 :(得分:1)
您可以尝试在TreeView的ItemContainerStyle中设置您想要控制TreeViewItem的可见性的绑定。可能您必须在HierarchicalDataTemplate中为每个级别的TreeViewItem重复该设置。如果Binding不起作用,您仍然可以在ItemContainerStyle中尝试Trigger或DataTrigger。可能你甚至不需要带有触发器的转换器。
答案 1 :(得分:0)
这是完成任务的代码:
<TreeView.Resources>
<Style TargetType="TreeViewItem" >
<Setter Property="Visibility" Value="{Binding Path=Layer,
Converter={StaticResource cIsDrawingLayer}}" />
</Style>
</TreeView.Resources>