I am trying to disable certain menu items in a treeview's context menu if the SelectedItem property for the treeview is null. My expectation is that this would be most simple to achieve by binding the SelectedItem property of the TreeView to the IsEnabled property of the MenuItem with a converter in between.
I'm not particularly experienced in WPF yet, so I'm guessing that I am doing something wrong with the binding.
I have found this solution to manually brute force the IsEnabled property whenever the menu is opened, but it doesn't seem an ideal solution to me. It would not be immediately clear to anyone adding new menu items that they need to modify the code behind to disable their new menu item if nothing is selected in the tree view. If what I am trying to achieve with binding (or some other XAML solution) is not possible, I will have go for this brute force solution.
Here's a stripped down example of what I have so far:
MainWindow.xaml
<Window x:Class="treeViewTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:treeViewTest="clr-namespace:treeViewTest"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<treeViewTest:NullObjectToDisabledConverter
x:Key="NullObjectToDisabledConverter" />
</Window.Resources>
<Grid>
<TreeView Name="treeView" MouseLeftButtonDown="treeView_MouseLeftButtonDown">
<TreeViewItem Header="Parent" IsExpanded="True">
<TreeViewItem Header="Child" />
</TreeViewItem>
<TreeViewItem Header="Sibling" />
<TreeView.ContextMenu>
<ContextMenu>
<MenuItem Header="Copy"
IsEnabled="{Binding ElementName=treeView, Path=SelectedItem,
Converter={StaticResource NullObjectToDisabledConverter}}" />
<MenuItem Header="Paste"
IsEnabled="{Binding ElementName=treeView, Path=SelectedItem,
Converter={StaticResource NullObjectToDisabledConverter}}" />
</ContextMenu>
</TreeView.ContextMenu>
</TreeView>
</Grid>
</Window>
MainWindow.xaml.cs
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
namespace treeViewTest
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void treeView_MouseLeftButtonDown(object sender,
MouseButtonEventArgs e)
{
TreeViewItem node = treeView.SelectedItem as TreeViewItem;
if (node != null)
{
node.IsSelected = false;
}
}
}
public class NullObjectToDisabledConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter,
CultureInfo culture)
{
return (value != null);
}
public object ConvertBack(object value, Type targetType, object parameter,
CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
I've tried returning false in the converter to brute force disable the menu items but the converter is never actually getting called.
Thanks in advance for any help with understanding why this does not work.