WPF - using different foreground colors for TextBlock

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

    Posted 09 Feb 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">
        <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

     

  2. Martin
    Admin
    Martin avatar
    1101 posts

    Posted 11 Feb 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.
    <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
  3. UI for WPF is Visual Studio 2017 Ready
Back to Top