0

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.

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");
    }
xxdefaultxx
  • 175
  • 1
  • 12
  • I can't get your example to show the context menu, could you simplify your example? – Adrian Nasui Dec 17 '14 at 08:37
  • 2
    or provide code to initialize the ItemSource – Adrian Nasui Dec 17 '14 at 08:38
  • Check if you have 'Show output from' set to 'Debug' in your 'Output' window. Because if you don't, then you won't be able to see your Console.WriteLine's in output window where you usualy expect to see them. – Steven Dec 17 '14 at 08:55

1 Answers1

1

From what I can tell, the problem is that you cannot attach a Click event like you did in a DataTemplate. 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. 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);
    }
}
Community
  • 1
  • 1