This is a migrated thread and some comments may be shown as answers.

WPF - using different foreground colors for TextBlock

1 Answer 358 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Christian
Top achievements
Rank 1
Christian asked on 09 Feb 2016, 09:08 AM

Hi @ll,

 

first: our basics

  • wpf application with implicit styles
  • user is allowed to change the theme on the fly

Now I want to use different foreground colors of a TextBlock

to illustrate a special meaning. Therefore I use the following style:

<Style TargetType="{x:Type TextBlock}" x:Key="CompareStatusStyle">
    <Style.Triggers>
        <DataTrigger Binding="{Binding Status}" Value="{x:Static vm:CompareStatus.Different}">
            <Setter Property="Foreground" Value="Orange"/>
        </DataTrigger>
        <DataTrigger Binding="{Binding Status}" Value="{x:Static vm:CompareStatus.OnlyA}">
            <Setter Property="Foreground" Value="Red"/>
        </DataTrigger>
        <DataTrigger Binding="{Binding Status}" Value="{x:Static vm:CompareStatus.OnlyB}">
            <Setter Property="Foreground" Value="ForestGreen"/>
        </DataTrigger>
        <DataTrigger Binding="{Binding Status}" Value="{x:Static vm:CompareStatus.Unknown}">
            <Setter Property="Foreground" Value="Gray"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

 

This works mostly very well.

But when I used this Style inside a RadTreeView (header) there are some

problems with background colors. (see the attached image)

 

My question: what is the best way to define colors which might be change

using a different theme or adapt automatically to the background.

 

I found a bad way: using code behind to change the background color

of the SelectionVisual of a RadTreeViewItem. But unfortunately not every

theme is build in the same way and the border defining the background

color is not in path from my content to the RadTreeItem, so finding the

current background color is hard.

 

Has anyone an idea how I can solve this?

 

Thanks a lot.

Best regards

Christian

 

1 Answer, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 11 Feb 2016, 03:04 PM
Hi Christian,

If we assume that you are using RadTreeView in a data bound scenario, a possible approach for achieving your requirement is to bind the Foreground color of the header element in the treeview's ItemTemplate.
<telerik:RadTreeView.ItemTemplate>
    <HierarchicalDataTemplate>
        <TextBlock Foreground="{Binding Status, Converter={StaticResource MyStatusToForegroundConverter}}" />
    </HierarchicalDataTemplate>
</telerik:RadTreeView.ItemTemplate>
You can find such approach demonstrated in the attached project. Please give it a try and let me know if it works for you.

Regards,
Martin
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
General Discussions
Asked by
Christian
Top achievements
Rank 1
Answers by
Martin Ivanov
Telerik team
Share this question
or