Question

I want to assign a new DataTrigger programmatically (in code/not xaml) to every "TreeViewItem" style like I do in the following xaml.

I did some code (under xaml) where I defined my trigger but "ItemContainerStyle" is null. The function is called on Window Initialize event.

Anybody has an idea what I'm doing wrong ?

EDIT

I found some part of my problem: I moved my style from the "Resource" section to the "ItemContainerStyle" section as defined below in code sample. This way the style is still applied and I can access the style from the TReeView.ItemContainerStyle property in code. But I still don't know how to get the TreeViewItem selected style like the color of the background by code ??

I have xaml:

                <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.Triggers>
                            <DataTrigger Binding="{Binding Path=IsHilighted}" Value="true">
                                <!--<Setter Property="Background" Value="SlateBlue"></Setter>-->
                                <Setter Property="Background" Value="{StaticResource {x:Static SystemColors.HighlightBrushKey}}"></Setter>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>

I have that code:

    public static void EnableMultipleSelection(TreeView treeView)
    {
        if (!_isMultiSelectTreeViewLeftButtonHandlerRegistered)
        {
            EventManager.RegisterClassHandler(typeof(TreeViewItem), UIElement.MouseDownEvent,
                                                             new MouseButtonEventHandler(TreeViewMouseDownGlobal));
            _isMultiSelectTreeViewLeftButtonHandlerRegistered = true;
        }

        DataTrigger dataTrigger = new DataTrigger();
        dataTrigger.Binding = new Binding("IsHilighted");
        dataTrigger.Value = true;
        dataTrigger.Setters.Add(new Setter(TreeViewItem.BackgroundProperty, new SolidColorBrush(Colors.Brown)));

        treeView.ItemContainerStyle.Triggers.Add(dataTrigger);

Edited code:

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

            <TreeView.ItemContainerStyle>
                <Style TargetType="TreeViewItem">
                    <Setter Property="IsExpanded" Value="{Binding Path=IsExpanded}"/>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Path=IsHilighted}" Value="true">
                            <Setter Property="Background" Value="{StaticResource {x:Static SystemColors.HighlightBrushKey}}"></Setter>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </TreeView.ItemContainerStyle>

            <TreeView.Resources>
                <!--<Style TargetType="TreeViewItem">
                    <Setter Property="IsExpanded" Value="{Binding Path=IsExpanded}"/>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Path=IsHilighted}" Value="true">
                            <Setter Property="Background" Value="{StaticResource {x:Static SystemColors.HighlightBrushKey}}"></Setter>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>-->
Was it helpful?

Solution

It sounds like you are trying to set the Style.Trigger too soon, before the template has been applied. If you apply the Trigger after the FrameWorkElement you are targeting has fully loaded you'll probably find that the ItemStyleContainer is no longer null.

http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.loaded.aspx

Implementing OnApplyTemplate for your FrameworkElement should hook you in after the ItemStyleContainer has been set as it is called after the Visual Tree has been rendered.

http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.onapplytemplate.aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top