Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Why don't you define the context menu of the items instead ?</p> <p>You could through a style define a TreeViewItem context menu by binding it to a static resource that eventually has x:Shared set to true. It will be defined in one place once and for all. Unless you have a very specific reason to make it happen at the treeview level, this way effectively does what you want : no menu on empty space.</p> <pre><code>&lt;Window x:Class="WpfApplication5.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"&gt; &lt;Grid&gt; &lt;Grid.Resources&gt; &lt;ContextMenu x:Key="menu" x:Shared="true"&gt; &lt;MenuItem Header="_Open" Click="MenuItemOpen_Click" /&gt; &lt;/ContextMenu&gt; &lt;Style TargetType="TreeViewItem"&gt; &lt;Setter Property="ContextMenu" Value="{StaticResource menu}"/&gt; &lt;/Style&gt; &lt;/Grid.Resources&gt; &lt;TreeView x:Name="treeView"&gt; &lt;TreeViewItem Header="item1"&gt; &lt;TreeViewItem Header="item2"&gt; &lt;TreeViewItem Header="item3"&gt; &lt;/TreeViewItem&gt; &lt;/TreeViewItem&gt; &lt;/TreeViewItem&gt; &lt;/TreeView&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <p>Tell us more about what you're trying to achieve otherwise.</p> <p><strong>EDIT</strong></p> <p>Notice that style has no x:Key therefore it applies by default to all TreeViewItems, if this isn't convenient for you as you might have different trees and items, you can explicitly assign the style at the TreeView level (ItemContainerStyle)</p> <p>However, you see that it doesn't propagate to childrens :</p> <p><img src="https://i.stack.imgur.com/ZiJQY.png" alt="enter image description here"></p> <p>But if you add a setter for the ItemContainerStyle property in that style, you can see that it gets propagated to childrens as well. (self-reference)</p> <p><img src="https://i.stack.imgur.com/3whbW.png" alt="enter image description here"></p> <pre><code>&lt;Window x:Class="WpfApplication5.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Width="382" Height="159"&gt; &lt;Window.Resources&gt; &lt;ContextMenu x:Key="menu" x:Shared="true"&gt; &lt;MenuItem Click="MenuItemOpen_Click" Header="_Open" /&gt; &lt;/ContextMenu&gt; &lt;Style x:Key="TreeViewItemStyle1" TargetType="{x:Type TreeViewItem}"&gt; &lt;Setter Property="ContextMenu" Value="{StaticResource menu}" /&gt; &lt;Setter Property="Background" Value="Red" /&gt; &lt;Setter Property="ItemContainerStyle" Value="{DynamicResource TreeViewItemStyle1}" /&gt; &lt;/Style&gt; &lt;/Window.Resources&gt; &lt;Grid&gt; &lt;TreeView x:Name="treeView" ItemContainerStyle="{DynamicResource TreeViewItemStyle1}"&gt; &lt;TreeViewItem Header="item1" IsExpanded="True"&gt; &lt;TreeViewItem Header="item2" IsExpanded="True"&gt; &lt;TreeViewItem Header="item3" IsExpanded="True" /&gt; &lt;/TreeViewItem&gt; &lt;/TreeViewItem&gt; &lt;/TreeView&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <p>Last but not least, you can define ItemContainerStyleSelector should you need red, blue or greens items according their content and still retain that inheritance from the style.</p>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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