0
votes

I have a WPF that uses a TreeView and a HierarchiacalDataTemplate. When I set styling for the different levels, sometimes the foreground color for the text is White and sometimes its Black (depending on the background color). Here's my dilemma. When the user selects an item in the treeview, wpf applies a border (which is fine) and changes the foreground color of the text. For the items that already have a white foreground, its fine but for the items that have a black foreground the text virtual disappears. I can't really use property setters and triggers because the style doesn't apply universally to all nodes, just to certain ones. How can I disable WPF from changing the foreground color at all OnSelection? Below is my WPF code:

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

2 Answers

0
votes

Set this style in Resources. Don't set any key it will be applied for all the TreeViewItems that satisfy the condition. Sample code:

<Style TargetType="TreeViewItem">
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="true">
                    <Setter Property="Foreground" Value="White"/>
                </Trigger>
           </Style.Triggers>
        </Style>
0
votes

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.