我有一个使用TreeView和HierarchiacalDataTemplate的WPF。当我为不同级别设置样式时,有时文本的前景色为白色,有时为黑色(取决于背景色)。这就是我的困境。当用户选择树视图中的项目时,wpf应用边框(这很好)并更改文本的前景色。对于已经具有白色前景的项目,它很好,但对于具有黑色前景的项目,文本虚拟消失。我无法真正使用属性设置器和触发器,因为该样式并不普遍适用于所有节点,只适用于某些节点。如何在所有OnSelection上禁用WPF以更改前景色?下面是我的WPF代码:
<TreeView Name="tvwConfig" VerticalAlignment="Stretch" DockPanel.Dock="Left" Width="300" Background="LightGray" ItemsSource="{Binding}" SelectedItemChanged="Treeview_SelectedItemChanged" >
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type cfg:AppConfig}" ItemsSource="{Binding Path=Services}">
<Border Width="200" BorderBrush="DarkBlue" Background="DarkBlue" BorderThickness="1" CornerRadius="2" Margin="2" Padding="2">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=ServerName}" FontWeight="Bold" Foreground="White" />
</StackPanel>
</Border>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type cfg:Service}" ItemsSource="{Binding Path=Queues}">
<Border Width="200" BorderBrush="RoyalBlue" Background="RoyalBlue" BorderThickness="1" CornerRadius="2" Margin="2" Padding="2">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Name}" FontWeight="Bold" Foreground="White" />
<TextBlock Text=" (" FontWeight="Bold" Foreground="White" />
<TextBlock Text="{Binding Path=Modality}" FontWeight="Bold" Foreground="White" />
<TextBlock Text=")" FontWeight="Bold" Foreground="White" />
</StackPanel>
</Border>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type cfg:Queue}" ItemsSource="{Binding Path=Statuses}">
<Border Width="200" BorderBrush="AliceBlue" Background="AliceBlue" BorderThickness="1" CornerRadius="2" Margin="2" Padding="2">
<StackPanel Orientation="Horizontal" >
<TextBlock Text="{Binding Path=Name}" FontWeight="SemiBold" />
</StackPanel>
</Border>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type cfg:Status}">
<Border Width="200" BorderBrush="White" Background="White" BorderThickness="1" CornerRadius="2" Margin="2" Padding="2">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Name}" />
<TextBlock Text=" ("/>
<TextBlock Text="{Binding Path=Weight}" />
<TextBlock Text=")" />
</StackPanel>
</Border>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
答案 0 :(得分:0)
在参考资料中设置此样式。不要设置任何键,它将应用于满足条件的所有TreeViewItem。 示例代码:
<Style TargetType="TreeViewItem">
<Style.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter Property="Foreground" Value="White"/>
</Trigger>
</Style.Triggers>
</Style>
答案 1 :(得分:0)
我现在无法对此进行测试,但我认为您应该能够在Style
中为ItemContainerStyle
定义HierarchicalDataTemplate
并定义样式(使用setter )根据需要在每一个。
例如(来自你的一个例子):
<HierarchicalDataTemplate DataType="{x:Type cfg:AppConfig}" ItemsSource="{Binding Path=Services}">
<!-- Example -->
<HierarchicalDataTemplate.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="FontWeight" Value="Normal" />
<Setter Property="Foreground" Value="DarkOrange" />
<!-- Triggers if required -->
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="Foreground" Value="DarkOrange" />
</Trigger>
<!-- DataTriggers if required -->
<DataTrigger Binding="{Binding Path=SomeProperty}" Value="SomeValue">
<Setter Property="Foreground" Value="Red" />
</DataTrigger>
</Style.Triggers>
</Style>
</HierarchicalDataTemplate.ItemContainerStyle>
<!-- End Example -->
<Border Width="200" BorderBrush="DarkBlue" Background="DarkBlue" BorderThickness="1" CornerRadius="2" Margin="2" Padding="2">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=ServerName}" FontWeight="Bold" Foreground="White" />
</StackPanel>
</Border>
</HierarchicalDataTemplate>
IsSelected
setter可以帮助您整理前景色。