Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to enable/disable menuitem (in contextmenu) for a selecteditem of treeview
    primarykey
    data
    text
    <p>I need to use a treeview control in WPF application I create nested data (with type DataItem) and assign it to treeview control. Create a hierarchicaldatatemplate and assign it to treeview control Add a contextmenu to treeview, Now I want to disable or enable the menu item by one property of SelectedItem (it should be DataItem, I think), "IsEnabled"</p> <p>How to approach that? </p> <pre><code> &lt;HierarchicalDataTemplate x:Key="dt" ItemsSource="{Binding Items}"&gt; &lt;TextBlock x:Uid="TextBlock_2" Text="{Binding Name}"&gt; &lt;/TextBlock&gt; &lt;/HierarchicalDataTemplate&gt; &lt;TreeView x:Name="tree_3" Height="200" ItemTemplate="{StaticResource dt}" &gt; &lt;TreeView.ContextMenu&gt; &lt;ContextMenu&gt; &lt;MenuItem ItemsSource="{Binding SelectedItem}" Header="doA" IsEnabled="{Binding IsEnabled, Mode=OneWay}" /&gt; &lt;MenuItem Header="doB" IsEnabled="False"/&gt; &lt;MenuItem Header="doC" /&gt; &lt;/ContextMenu&gt; &lt;/TreeView.ContextMenu&gt; &lt;/TreeView&gt; class DataItem : DependencyObject //INotifyPropertyChanged { private List&lt;DataItem&gt; _items = new List&lt;DataItem&gt;(); public List&lt;DataItem&gt; Items { get { return _items; } } public string Name { get; set; } public int Category { get; set; } public bool IsEnabled { get; set; } } private void Window_Loaded(object sender, RoutedEventArgs e) { List&lt;DataItem&gt; allData = new List&lt;DataItem&gt;(); DataItem i1 = new DataItem() { Name = "Test1", Category = 1, IsEnabled = false }; DataItem i2 = new DataItem() { Name = "Test2", Category = 2, IsEnabled = false }; DataItem i3 = new DataItem() { Name = "Test3", Category = 3, IsEnabled = true }; DataItem t1 = new DataItem() { Name = "Template 1", Category = 1, IsEnabled = true }; DataItem t2 = new DataItem() { Name = "Template 2", Category = 1, IsEnabled = false }; i1.Items.Add(t1); i1.Items.Add(t2); allData.Add(i1); allData.Add(i2); allData.Add(i3); tree_3.ItemsSource = allData; } </code></pre> <p>I resolved the problem by the following method. </p> <ol> <li><p>create a context menu resource</p> <pre><code>&lt;ContextMenu x:Uid="ContextMenu_1" x:Key="TreeViewItemContextMenu" ItemsSource="{Binding}"&gt; &lt;MenuItem x:Uid="MenuItem_1" Header="Rename" IsEnabled="{Binding Path=., Converter={StaticResource renameMenuConverter}}" Click="RenameMenu_OnClick" /&gt; &lt;/ContextMenu&gt; </code></pre></li> <li><p>create a style for the tree view item and using the context menu resource above </p> <pre><code>&lt;Style TargetType="{x:Type TreeViewItem}" x:Uid="Style_1"&gt; &lt;Setter x:Uid="Setter_32" Property="ContextMenu" Value="{StaticResource TreeViewItemContextMenu}"&gt; &lt;/Setter&gt; ... &lt;/Style&gt; </code></pre></li> <li><p>implement a converter. According to the markup extension above, the parmeter passed in convert is the current selected item, so I can get the property of it and do the checking. </p></li> </ol> <p>thanks for all of your help. </p>
    singulars
    1. This table or related slice is empty.
    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.
 

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