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?
问问题
2217 次
2 回答
5
我假设您将您的绑定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}" />
于 2013-08-15T03:41:11.030 回答
1
您可以结合使用几个技巧来做到这一点 - 如果它是创建一个 IValueConverter 的要点,它允许您从 HierarchicalDataTemplate 内部传递一个 FrameworkElement 并确定保存当前项目的 TreeViewItem 是否是顶级 TreeViewItem。这是通过遍历 wpf 应用程序的 VisualTree 并找到重要的 TreeViewItem 来实现的。如果给定的 TreeViewItem 没有 TreeViewItem 类型的祖先,那么您知道它必须是顶级项。我们可以在 Style 中使用此信息来设置 TreeView 项上的 ContextMenu 的值。这是代码:
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>
代码隐藏和视图模型:
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();
}
}
于 2013-08-15T04:18:00.227 回答