wpf - 我在设置树视图根节点的背景颜色时遇到问题。
问题描述
我有一个绑定到分层数据模板的树视图控件。对于根节点,我必须隐藏扩展器控件,而对于后续级别,我必须显示扩展器控件。
我的根节点包含一个切换按钮和文本块控件。不知何故,我无法为根节点正确设置背景颜色。
Xaml 页面:
<StackPanel Name="Stack2" Margin="0,-20,0,0" Orientation="Horizontal">
<TreeView x:Name="MainTreeView" HorizontalContentAlignment="Stretch" Background="{StaticResource White1}"
ItemContainerStyle="{StaticResource MyTreeView1}" BorderThickness="0" ItemsSource="{Binding RootNodeList}"
HorizontalAlignment="Stretch" Margin="0,20,0,0" VerticalAlignment="Stretch">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemContainerStyle="{StaticResource MyTreeView}" ItemsSource="{Binding ComponentTypesNodes}">
<StackPanel Orientation="Vertical">
<StackPanel Name="Stack1" Orientation="Horizontal" HorizontalAlignment="Stretch" Margin="-20,15,0,0" Background="{StaticResource White1}">
<ToggleButton Margin="10,0,0,0" Name="Tg1" Template="{Binding ControlTemplate,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Click="Tg1_Click"/>
<TextBlock Name="lblName" Margin="30,0,0,0" Text="{Binding RootNodeName}" Style="{StaticResource TreeViewRootNode}"/>
<Grid Name="ItemSelectedIndicator" Grid.ColumnSpan="2" Grid.Column="0" Background="{StaticResource White6}"
Visibility="Collapsed" />
</StackPanel>
<Separator Margin="0,0,0,0" Height="10" Background="{StaticResource White1}" Width="300"/>
</StackPanel>
<HierarchicalDataTemplate.ItemTemplate>
<HierarchicalDataTemplate ItemContainerStyle="{StaticResource MyTreeView}" ItemsSource="{Binding ComponentList}" >
<TextBlock Text="{Binding ComponentType}" HorizontalAlignment="Stretch" Padding="6,6,0,0" Style="{StaticResource TreeViewRootNode}"/>
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding ComponentName}" HorizontalAlignment="Stretch" Padding="0,6,0,0" Style="{StaticResource TreeViewRootNode}"/>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
</StackPanel>
<Border Name="BrdPin" Background="Gray" Margin="-70,-620,30,0" Height="30" Width="30" HorizontalAlignment="Right">
<ToggleButton Name="TogglePin" Template="{StaticResource Pin}" Click="TogglePin_Click" />
</Border>
</StackPanel>
样式页面如下:
<Style TargetType="{x:Type TreeViewItem}" x:Key="MyTreeView">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
<Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
<Setter Property="Padding" Value="0,0,0,0"/>
<Setter Property="Foreground" Value="{StaticResource Gray4}"/>
<Setter Property="ToggleButton.IsChecked" Value="True"/>
<Setter Property="FocusVisualStyle" Value="{StaticResource TreeViewItemFocusVisual}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TreeViewItem}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition MinWidth="19" Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<ToggleButton Margin="30,5,0,0" x:Name="Expander" ClickMode="Press" IsChecked="{Binding IsExpanded, RelativeSource={RelativeSource TemplatedParent}}" Style="{StaticResource ExpandCollapseToggleStyle}"/>
<Border x:Name="Bd" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Grid.Column="1" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
<ContentPresenter x:Name="PART_Header" ContentSource="Header" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" Margin="0,0,0,0"/>
</Border>
<ItemsPresenter HorizontalAlignment="Stretch" x:Name="ItemsHost" Grid.ColumnSpan="2" Grid.Column="1" Grid.Row="1"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsExpanded" Value="false">
<Setter Property="Visibility" TargetName="ItemsHost" Value="Collapsed"/>
</Trigger>
<Trigger Property="HasItems" Value="false">
<Setter Property="Visibility" TargetName="Expander" Value="Hidden"/>
</Trigger>
<Trigger Property="IsSelected" Value="true">
<Setter Property="Background" TargetName="Bd" Value="{StaticResource White6}"/>
<Setter Property="TextBlock.Foreground" Value="{StaticResource Blue}"/>
<Setter Property="ToggleButton.IsChecked" Value="True"/>
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="true"/>
<Condition Property="IsSelectionActive" Value="false"/>
</MultiTrigger.Conditions>
<Setter Property="Background" TargetName="Bd" Value="{StaticResource White6}"/>
<Setter Property="TextBlock.Foreground" Value="Green"/>
<Setter Property="ToggleButton.IsChecked" Value="True"/>
</MultiTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="VirtualizingStackPanel.IsVirtualizing" Value="true">
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<VirtualizingStackPanel/>
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Foreground" Value="{StaticResource Blue}"/>
<Setter Property="ToggleButton.IsChecked" Value="True"/>
</Trigger>
</Style.Triggers>
</Style>
<Style x:Key="ExpandCollapseToggleStyle1" TargetType="{x:Type ToggleButton}">
<Setter Property="Focusable" Value="False"/>
<Setter Property="Width" Value="16"/>
<Setter Property="Height" Value="16"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type TreeViewItem}" x:Key="MyTreeView1">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
<Setter Property="Padding" Value="0,0,0,0"/>
<Setter Property="Foreground" Value="{StaticResource Gray4}"/>
<Setter Property="Opacity" Value="100"/>
<Setter Property="FocusVisualStyle" Value="{StaticResource TreeViewItemFocusVisual}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TreeViewItem}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition MinWidth="19" Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Border x:Name="Bd" VerticalAlignment="Stretch" BorderThickness="0" Background="{TemplateBinding Background}" Grid.Column="1" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
<ContentPresenter x:Name="PART_Header" VerticalAlignment="Stretch" ContentSource="Header" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" Margin="0,0,0,0"/>
</Border>
<ItemsPresenter HorizontalAlignment="Stretch" VerticalAlignment="Stretch" x:Name="ItemsHost" Grid.ColumnSpan="2" Grid.Column="1" Grid.Row="1"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsExpanded" Value="false">
<Setter Property="Visibility" TargetName="ItemsHost" Value="Collapsed"/>
</Trigger>
<Trigger Property="IsSelected" Value="true">
<Setter Property="Panel.Background" TargetName="Bd" Value="Red"/>
<Setter Property="TextBlock.Foreground" Value="Green"/>
<Setter Property="ToggleButton.IsChecked" Value="True"/>
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="true"/>
<Condition Property="IsSelectionActive" Value="false"/>
</MultiTrigger.Conditions>
<Setter Property="Background" TargetName="Bd" Value="Transparent"/>
<Setter Property="TextBlock.Foreground" Value="Green"/>
<Setter Property="ToggleButton.IsChecked" Value="True"/>
</MultiTrigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="Transparent"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="VirtualizingStackPanel.IsVirtualizing" Value="true">
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<VirtualizingStackPanel/>
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Foreground" Value="{StaticResource Blue}"/>
<Setter Property="ToggleButton.IsChecked" Value="True"/>
</Trigger>
</Style.Triggers>
</Style>`
解决方案
推荐阅读
- entitlements - 将数据推送到 Open Policy Agent 时如何处理容器重启
- json - Angular,如何订阅嵌套的 JSON 对象?
- odata - 如何将 $filter 添加到 XML 中的 OData 聚合绑定
- javascript - 如何重用一个组件确实在多个组件之间挂载了等效的钩子
- xml - XSL 将 XML 转换为更小的 XML
- node.js - 使用 NodeJS/Puppeteer 下载多个图像
- python - 为什么在尝试计算具有较大初始值的值时总是出现溢出错误?
- python - 使用 Cmake 配置暗网时出错
- linux - “中断处理程序的中断服务程序”是什么意思?
- javascript - 在 Vue App 中使用动态目录的 webpack require.context() 的解决方法