0

我有一个使用 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>
4

2 回答 2

0

I'm not able to test this right now, but I think you should be able to define your Style in an ItemContainerStyle for your HierarchicalDataTemplate and define the style (with setters) in each one as required.

E.g (from one of your examples):

<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>

The IsSelected setter should help you sort out the foreground colours.

于 2013-11-08T20:51:17.980 回答
0

在资源中设置此样式。不要设置任何键,它将应用于所有满足条件的 TreeViewItem。示例代码:

<Style TargetType="TreeViewItem">
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="true">
                    <Setter Property="Foreground" Value="White"/>
                </Trigger>
           </Style.Triggers>
        </Style>
于 2013-11-08T04:07:36.473 回答