Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF - TreeView - How to programmatically extract the style of a TreeViewItem (selected style)
    primarykey
    data
    text
    <p>I want to assign a new DataTrigger programmatically (in code/not xaml) to every "TreeViewItem" style like I do in the following xaml. </p> <p>I did some code (under xaml) where I defined my trigger but "ItemContainerStyle" is null. The function is called on Window Initialize event.</p> <p>Anybody has an idea what I'm doing wrong ?</p> <p><strong>EDIT</strong></p> <p>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 ??</p> <p>I have xaml:</p> <pre><code> &lt;TreeView Name="TreeViewSelectScopeStudy" MinHeight="24" Margin="7" ItemsSource="{Binding Path=TvItemRootPssTreeViewRoot.ChildsView}" Height="Auto" VerticalAlignment="Stretch" VirtualizingStackPanel.IsVirtualizing="True" VirtualizingStackPanel.VirtualizationMode="Recycling"&gt; &lt;TreeView.Resources&gt; &lt;Style TargetType="TreeViewItem"&gt; &lt;Setter Property="IsExpanded" Value="{Binding Path=IsExpanded}"/&gt; &lt;Style.Triggers&gt; &lt;DataTrigger Binding="{Binding Path=IsHilighted}" Value="true"&gt; &lt;!--&lt;Setter Property="Background" Value="SlateBlue"&gt;&lt;/Setter&gt;--&gt; &lt;Setter Property="Background" Value="{StaticResource {x:Static SystemColors.HighlightBrushKey}}"&gt;&lt;/Setter&gt; &lt;/DataTrigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; </code></pre> <p>I have that code:</p> <pre><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); </code></pre> <p><strong>Edited code:</strong></p> <pre><code> &lt;TreeView Name="TreeViewSelectScopeStudy" MinHeight="24" Margin="7" ItemsSource="{Binding Path=TvItemRootPssTreeViewRoot.ChildsView}" Height="Auto" VerticalAlignment="Stretch" VirtualizingStackPanel.IsVirtualizing="True" VirtualizingStackPanel.VirtualizationMode="Recycling"&gt; &lt;TreeView.ItemContainerStyle&gt; &lt;Style TargetType="TreeViewItem"&gt; &lt;Setter Property="IsExpanded" Value="{Binding Path=IsExpanded}"/&gt; &lt;Style.Triggers&gt; &lt;DataTrigger Binding="{Binding Path=IsHilighted}" Value="true"&gt; &lt;Setter Property="Background" Value="{StaticResource {x:Static SystemColors.HighlightBrushKey}}"&gt;&lt;/Setter&gt; &lt;/DataTrigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; &lt;/TreeView.ItemContainerStyle&gt; &lt;TreeView.Resources&gt; &lt;!--&lt;Style TargetType="TreeViewItem"&gt; &lt;Setter Property="IsExpanded" Value="{Binding Path=IsExpanded}"/&gt; &lt;Style.Triggers&gt; &lt;DataTrigger Binding="{Binding Path=IsHilighted}" Value="true"&gt; &lt;Setter Property="Background" Value="{StaticResource {x:Static SystemColors.HighlightBrushKey}}"&gt;&lt;/Setter&gt; &lt;/DataTrigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt;--&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload