我有一个项目的 TreeView
,我想让一个 ContextMenu
仅在第二层项目中弹出。我该如何实现这个功能呢?
我有一个项目的 TreeView
,我想让一个 ContextMenu
仅在第二层项目中弹出。我该如何实现这个功能呢?
我假设你正在将你的 TreeView
绑定到一个项目列表。如果是这样,第一层和第二层的项目可以是不同的数据类型吗?那么,你可以对第一层类型使用 HierarchicalDataTemplate
,对第二层类型使用 DataTemplate
,如下所示:
<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}" />
<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>
代码后台和视图模型: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();
}
}