Question

I have a TreeView where the data is bound to generic derived wrapper classes over my data hierarchy. My bound wrapper classes include added fields like "IsHilighted" and "IsExpanded".

I would like to change the background of any TreeViewItem according to its bound data property "IsHiglighted". I would like to set the color of Hilighted item to the same (or lighter) color as the default Selected item background color.

Ideally, I would like to not modify existing XAML... I mean being able to eventually add the behavior through code.

UPDATE

I have found a partial solution: I had to add triggers as defined below. Code included below.

        <Style TargetType="TreeViewItem">
            <Setter Property="IsExpanded" Value="{Binding Path=IsExpanded}"/>

            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=IsHilighted}" Value="true">
                    <Setter Property="Background" Value="SlateBlue"></Setter>
                    <Setter Property="Opacity" Value="160"></Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>

Still not resolved: How could I bind the color of Hilighted item (see partial solution above) to the "Selected" TreeViewItem background color, i.e. replace "SlateBlue" on partial solution to binding to existing selected item style background color ?

Original TreeView XAML code:

<TreeView Name="TreeViewSelectScopeStudy" MinHeight="24" Margin="7" ItemsSource="{Binding Path=TvItemRootPssTreeViewRoot.ChildsView}" Height="Auto"
    VerticalAlignment="Stretch"
    VirtualizingStackPanel.IsVirtualizing="True"
    VirtualizingStackPanel.VirtualizationMode="Recycling">

    <TreeView.Resources>

        <Style TargetType="TreeViewItem">
            <Setter Property="IsExpanded" Value="{Binding Path=IsExpanded}"/>
        </Style>


        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red" />
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Green" />

        <HierarchicalDataTemplate DataType="{x:Type scopeSelection:WrapperSimulatedInfoStudy}" ItemsSource="{Binding Path=Childs}">
            <StackPanel Orientation="Horizontal">
                <CheckBox IsChecked="{Binding Path=IsSelected}"></CheckBox>
                <TextBlock Text="{Binding Path=TvItemName}" Margin="5,0,0,0"></TextBlock>
            </StackPanel>
        </HierarchicalDataTemplate>

        <HierarchicalDataTemplate DataType="{x:Type scopeSelection:WrapperSimulatedInfoSimulation}">
            <StackPanel Orientation="Horizontal" ToolTip="{Binding Path=Item.InvalidityReason}">
                <StackPanel.Style>
                    <Style TargetType="{x:Type StackPanel}">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Path=Item.IsValid}" Value="false">
                                <Setter Property="Opacity" Value="160"></Setter>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </StackPanel.Style>
                <CheckBox IsChecked="{Binding Path=IsSelected}" IsEnabled="{Binding Path=Item.IsValid}" ToolTip="{Binding Path=Item.InvalidityReason}"></CheckBox>
                <CheckBox IsChecked="{Binding Path=IsHilighted}"></CheckBox>
                <TextBlock Text="{Binding Path=TvItemName}" Margin="5,0,0,0" ToolTip="{Binding Path=Item.InvalidityReason}">
                    <TextBlock.Style>
                        <Style TargetType="{x:Type TextBlock}">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding Path=Item.IsValid}" Value="false">
                                    <Setter Property="Background" Value="LightPink"></Setter>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </TextBlock.Style>
                </TextBlock>
            </StackPanel>
        </HierarchicalDataTemplate>
    </TreeView.Resources>
</TreeView>
Was it helpful?

Solution

You could define one more property called IsItemSelected and bind it to TreeViewItems IsSelected property (similar to how you have done for IsExpanded).

<Style TargetType="TreeViewItem">
    <Setter Property="IsExpanded" Value="{Binding Path=IsExpanded}"/>
    <Setter Property="IsSelected" Value="{Binding Path=IsItemSelected}"/>
</Style>

Then you could define a DataTrigger for the IsItemSelected property and set the background color.

<DataTrigger Binding="{Binding Path=IsItemSelected}" Value="true">
    <Setter Property="Background" Value="Blue"></Setter>
</DataTrigger>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top