James Joshua Street
James Joshua Street

Reputation: 3419

WPF how to change contextmenu items based on treeviewitem type?

I have a TreeView of items and I want a ContextMenu to pop up only for the second tier items. How do I go about doing that?

Upvotes: 3

Views: 2372

Answers (2)

Andrew
Andrew

Reputation: 1492

You can do it with a mix of a couple tricks - the gist of if it is to create a IValueConverter that allows you to pass a FrameworkElement from inside your HierarchicalDataTemplate and determine if the TreeViewItem holding your current item is a top-level TreeViewItem. This is achieved by walking up the VisualTree of the wpf application and finding the important TreeViewItem. If a given TreeViewItem has no ancestor of type TreeViewItem, then you know it must be a top-level item. We can use this information in a Style to set the value of the ContextMenu on the TreeView items. Here's the code:

The XAML:

<Window x:Class="TreeViewContextMenu.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:TreeViewContextMenu"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Grid.Resources>
        <local:TreeViewItemToTopLevelConverter x:Key="treeViewConverter"/>
        <ContextMenu x:Key="contextMenu">
            <MenuItem Header="MenuItemHeader"/>
        </ContextMenu>
    </Grid.Resources>
    <TreeView ItemsSource="{Binding Items}">
        <TreeView.Resources>
            <HierarchicalDataTemplate DataType="{x:Type local:Part}" ItemsSource="{Binding SubParts}">
                <TextBlock Text="{Binding Name}">
                    <TextBlock.Style>
                        <Style TargetType="TextBlock">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource treeViewConverter}}" Value="False">
                                    <DataTrigger.Setters>
                                        <Setter Property="ContextMenu" Value="{StaticResource contextMenu}"/>
                                    </DataTrigger.Setters>
                                </DataTrigger> 
                            </Style.Triggers>
                        </Style>
                    </TextBlock.Style>
                </TextBlock>
            </HierarchicalDataTemplate>
        </TreeView.Resources>
    </TreeView>
</Grid>

The Code-behind and ViewModels:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = new ViewModel();
    }
}

public class ViewModel : PropertyChangedNotifier
{
    public ViewModel()
    {
        Items = new ObservableCollection<Part>();
        var parent = new Part() { Name = "Parent" };
        parent.SubParts = new ObservableCollection<Part>();
        parent.SubParts.Add(new Part() { Name = "Child1" });
        parent.SubParts.Add(new Part() { Name = "Child2" });

        Items.Add(parent);
    }

    private ObservableCollection<Part> _items;

    public ObservableCollection<Part> Items
    {
        get
        {
            return _items;
        }
        set
        {
            _items = value;
            OnPropertyChanged("Items");
        }
    }
}

public class Part : PropertyChangedNotifier
{
    private string _name;
    private ObservableCollection<Part> _subParts;

    public string Name
    {
        get
        {
            return _name;
        }
        set
        {
            _name = value;
            OnPropertyChanged("Name");
        }
    }

    public ObservableCollection<Part> SubParts
    {
        get { return _subParts; }
        set
        {
            _subParts = value;
            OnPropertyChanged("SubParts");
        }
    }

}

public class PropertyChangedNotifier : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(string propertyName)
    {
        var propertyChanged = PropertyChanged;
        if (propertyChanged != null)
        {
            propertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

public static class VisualTreeTools
{
    public static T GetVisualParent<T>(DependencyObject item) where T : DependencyObject
    {
        while (item != null)
        {
            item = VisualTreeHelper.GetParent(item);
            if (item is T)
                return item as T;
        }

        return null;
    }
}

public class TreeViewItemToTopLevelConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var item = value as DependencyObject;
        if (item == null)
            return 0;

        var containerTreeViewItem = VisualTreeTools.GetVisualParent<TreeViewItem>(item);
        var parentTreeViewItem = VisualTreeTools.GetVisualParent<TreeViewItem>(containerTreeViewItem);

        return parentTreeViewItem == null;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Upvotes: 1

Dave Clemmer
Dave Clemmer

Reputation: 3781

I'm assuming you are binding your TreeView to a list of items. If so, are or can the first and second tier of items be of different data types? Then, you can do a HierarchicalDataTemplate for your first tier type and a DataTemplate for your second tier type as such:

<HierarchicalDataTemplate DataType="{x:Type local:FirstTierType}" ItemsSource="{Binding Items}">
    <StackPanel Orientation="Horizontal">
        <TextBlock Text="{Binding Name}"  />
    </StackPanel>
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type local:SecondTierType}">
    <StackPanel Orientation="Horizontal">
        <TextBlock Text="{Binding Name}"  />
        <StackPanel.ContextMenu>
            <ContextMenu>
               <MenuItem Header="whatever1" Command="whatever1cmd"></MenuItem>
               <MenuItem Header="whatever2" Command="whatever2cmd"></MenuItem>
               <MenuItem Header="whatever3" Command="whatever2cmd"></MenuItem>
            </ContextMenu>
        </StackPanel.ContextMenu>
    </StackPanel>
</DataTemplate>
.
.
.
<TreeView ItemsSource="{Binding Items}" />

Upvotes: 5

Related Questions