WPF - using different foreground colors for TextBlock

2 posts, 0 answers
  1. Christian
    Christian avatar
    3 posts
    Member since:
    Sep 2015

    Posted 09 Feb 2016 Link to this post

    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">
            <DataTrigger Binding="{Binding Status}" Value="{x:Static vm:CompareStatus.Different}">
                <Setter Property="Foreground" Value="Orange"/>
            <DataTrigger Binding="{Binding Status}" Value="{x:Static vm:CompareStatus.OnlyA}">
                <Setter Property="Foreground" Value="Red"/>
            <DataTrigger Binding="{Binding Status}" Value="{x:Static vm:CompareStatus.OnlyB}">
                <Setter Property="Foreground" Value="ForestGreen"/>
            <DataTrigger Binding="{Binding Status}" Value="{x:Static vm:CompareStatus.Unknown}">
                <Setter Property="Foreground" Value="Gray"/>


    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



  2. Martin Ivanov
    Martin Ivanov avatar
    2474 posts

    Posted 11 Feb 2016 Link to this post

    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.
            <TextBlock Foreground="{Binding Status, Converter={StaticResource MyStatusToForegroundConverter}}" />
    You can find such approach demonstrated in the attached project. Please give it a try and let me know if it works for you.

    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
Back to Top