WPF如何根据treeviewitem类型更改contextmenu项?

时间:2013-08-15 01:17:01

标签: c# wpf xaml treeview contextmenu

我有TreeView个项目,我希望只为第二层项目弹出ContextMenu。我该怎么做呢?

2 个答案:

答案 0 :(得分: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}" />

答案 1 :(得分:1)

你可以混合使用几个技巧 - 如果要创建一个IValueConverter,它允许你从HierarchicalDataTemplate中传递FrameworkElement并确定持有当前项目的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>

Code-behind和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();
    }
}