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"
How to approach that?
<HierarchicalDataTemplate x:Key="dt" ItemsSource="{Binding Items}">
<TextBlock x:Uid="TextBlock_2" Text="{Binding Name}">
</TextBlock>
</HierarchicalDataTemplate>
<TreeView x:Name="tree_3" Height="200"
ItemTemplate="{StaticResource dt}"
>
<TreeView.ContextMenu>
<ContextMenu>
<MenuItem ItemsSource="{Binding SelectedItem}"
Header="doA" IsEnabled="{Binding IsEnabled, Mode=OneWay}" />
<MenuItem Header="doB" IsEnabled="False"/>
<MenuItem Header="doC" />
</ContextMenu>
</TreeView.ContextMenu>
</TreeView>
class DataItem : DependencyObject //INotifyPropertyChanged
{
private List<DataItem> _items = new List<DataItem>();
public List<DataItem> 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<DataItem> allData = new List<DataItem>();
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;
}
I resolved the problem by the following method.
create a context menu resource
<ContextMenu x:Uid="ContextMenu_1" x:Key="TreeViewItemContextMenu"
ItemsSource="{Binding}">
<MenuItem x:Uid="MenuItem_1"
Header="Rename"
IsEnabled="{Binding Path=., Converter={StaticResource renameMenuConverter}}"
Click="RenameMenu_OnClick"
/>
</ContextMenu>
create a style for the tree view item and using the context menu resource above
<Style TargetType="{x:Type TreeViewItem}" x:Uid="Style_1">
<Setter x:Uid="Setter_32" Property="ContextMenu" Value="{StaticResource TreeViewItemContextMenu}">
</Setter>
...
</Style>
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.
thanks for all of your help.
I have used this in WinForm TreeView with successful results.
When declaring the DataItems also include a tag property in them.
DataItem i1 = new DataItem() { Name = "Test1",
Category = 1, IsEnabled = false, Tag = "Test1" };
On the EventListener for the TreeView, cast sender object or get the SelectedItem from the TreeView.
//It is only Algorithm:
if treeview1.selecedItem.Tag = "Test1" then
//code for execution
end if
Similarly for the sender object,
//It is only Algorithm:
variable treeNode = cast ( sender to TreeView ).SelectedItem
if treeNode.Tag = "Test1" then
//code for execution
end if
As for the "code for execution" you can enable or disable the MenuItems.
NOTE: You haven't declared x:Name attribute for the MenuItems in the ContextMenu Tag. Please do that to access the MenuItems.
IsEnabled is the direct property for enabling or disabling the interactable items in WPF
I just gave the algorithm because, I'm more of a VB guy than C#. SO I don't want to mess up with the codes.
Have a great day :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With