简体   繁体   English

wpf treeview xaml上下文菜单单击事件未触发

[英]wpf treeview xaml context menu click event not firing

for some reason unbeknownst to me I have been having some trouble getting this click event firing from the context menu of a datasourced treeviewitem. 出于某种我不知道的原因,我在从数据源treeviewitem的上下文菜单中触发此click事件时遇到了一些麻烦。

The context menu appears as expected but is not handling its click events ( or at least not in the form I can see/retrieve ). 上下文菜单按预期显示,但未处理其单击事件(或至少没有以我能看到/检索的形式)。

<UserControl x:Class="Pipeline_General.Custom_Controls.ProjectTree"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:pm="clr-namespace:Pipeline_General"
         mc:Ignorable="d" 
         DataContext = "{Binding RelativeSource={RelativeSource Self}}"
         d:DesignHeight="300" d:DesignWidth="300">
<Grid>
    <TreeView Name="StructureTree" Background="{x:Static pm:myBrushes.defaultBG}" ItemsSource="{Binding ProjectList}">
        <TreeView.ItemContainerStyle>
            <Style TargetType="{x:Type TreeViewItem}">
                <Setter Property="IsExpanded" Value="True"/>
                <Setter Property="ContextMenu">
                    <Setter.Value>
                        <ContextMenu Background="{x:Static pm:myBrushes.defaultBG}" Foreground="{x:Static pm:myBrushes.gray}">
                            <MenuItem Header="Add Episode.." Click="AddEp"/>
                            <MenuItem Header="Add Sequence.." Click="AddSeq"/>
                            <MenuItem Header="Add Scene.." Click="AddScene"/>
                        </ContextMenu>
                    </Setter.Value>
                </Setter>
            </Style>
        </TreeView.ItemContainerStyle>
        <TreeView.ItemTemplate>
            <HierarchicalDataTemplate DataType="{x:Type pm:ProjectRoot}" ItemsSource="{Binding Episodes}">
                <TextBlock Text="{Binding Path=Title}" Foreground="{x:Static pm:myBrushes.pink}"
                           FontFamily="Calibri" FontSize="18"/>
                <HierarchicalDataTemplate.ItemContainerStyle>
                    <Style TargetType="{x:Type TreeViewItem}">
                        <Setter Property="ContextMenu">
                            <Setter.Value>
                                <ContextMenu Background="{x:Static pm:myBrushes.defaultBG}" Foreground="{x:Static pm:myBrushes.gray}">
                                    <MenuItem Header="Add Sequence.."  Click="AddSeq"/>
                                    <MenuItem Header="Add Scene.." Click="AddScene"/>
                                </ContextMenu>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </HierarchicalDataTemplate.ItemContainerStyle>


                <HierarchicalDataTemplate.ItemTemplate>
                    <HierarchicalDataTemplate DataType="{x:Type pm:Episode}" ItemsSource="{Binding Sequences}">
                        <TextBlock Text="{Binding Path=Key}" Foreground="{x:Static pm:myBrushes.orange}"
                                   FontFamily="Calibri" FontSize="16"/>
                        <HierarchicalDataTemplate.ItemTemplate>
                            <HierarchicalDataTemplate DataType="{x:Type pm:Sequence}" ItemsSource="{Binding Scenes}">
                                <TextBlock Text="{Binding Path=Key}" Foreground="{x:Static pm:myBrushes.yellow}"
                                           FontFamily="Calibri" FontSize="14"/>


                                <HierarchicalDataTemplate.ItemTemplate>
                                    <HierarchicalDataTemplate DataType="{x:Type pm:Scene}">
                                        <TextBlock Text="{Binding Path=Key}" Foreground="{x:Static pm:myBrushes.yellow}"
                                           FontFamily="Calibri" FontSize="14"/>
                                    </HierarchicalDataTemplate>
                                </HierarchicalDataTemplate.ItemTemplate>
                            </HierarchicalDataTemplate>
                        </HierarchicalDataTemplate.ItemTemplate>
                    </HierarchicalDataTemplate>
                </HierarchicalDataTemplate.ItemTemplate>
            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>
    </TreeView>
</Grid>

and in code behind... 并在后面的代码中...

public void AddSeq(object sender, RoutedEventArgs e)
    {
       var item = (TreeViewItem)StructureTree.SelectedItem;
       Console.WriteLine(item.Header.ToString());
    }
    public void AddEp(object sender, RoutedEventArgs e)
    {
        Console.WriteLine(e.OriginalSource.ToString());
        Console.WriteLine(e.Source.ToString());
        Console.WriteLine("EP");
    }
    public void AddScene(object sender, RoutedEventArgs e)
    {
        Console.WriteLine(e.OriginalSource.ToString());
        Console.WriteLine(e.Source.ToString());
        Console.WriteLine("Scene");
    }

From what I can tell, the problem is that you cannot attach a Click event like you did in a DataTemplate. 据我所知,问题在于您无法像在DataTemplate中那样附加Click事件。 You can refer to Event handler in DataTemplate to see how to do this, but what would be even better is to use a Command. 您可以在DataTemplate中引用事件处理程序以了解如何执行此操作,但最好使用Command。 This way your menu items will look like this: 这样,您的菜单项将如下所示:

<MenuItem Header="Add Episode.." Command="{x:Static throwAwayApp:MyCommands.AddEppCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Self}}"/>

and you would add a command like this: 您将添加如下命令:

public static class MyCommands
{
    static MyCommands()
    {
        AddEppCommand = new SimpleDelegateCommand(p =>
        {
            var menuItem = p as MenuItem;
            //Console.WriteLine(e.OriginalSource.ToString());
            //Console.WriteLine(e.Source.ToString());
            Console.WriteLine("EP");
        });
    }

    public static ICommand AddEppCommand { get; set; }
}

public class SimpleDelegateCommand : ICommand
{
    public SimpleDelegateCommand(Action<object> executeAction)
    {
        _executeAction = executeAction;
    }

    private Action<object> _executeAction;

    public bool CanExecute(object parameter)
    {
        return true;
    }

    public event EventHandler CanExecuteChanged;

    public void Execute(object parameter)
    {
        _executeAction(parameter);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM