出于某些原因,我一直不知道从数据源treeviewitem的上下文菜单中触发此单击事件时遇到了一些麻烦。
上下文菜单按预期显示,但未处理其点击事件(或至少不是我可以看到/检索的形式)。
<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>
并且代码背后......
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");
}
答案 0 :(得分:1)
据我所知,问题在于您无法像在DataTemplate中那样附加Click事件。您可以参考Event handler in DataTemplate来了解如何执行此操作,但更好的方法是使用命令。这样您的菜单项将如下所示:
<MenuItem Header="Add Episode.." Command="{x:Static throwAwayApp:MyCommands.AddEppCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Self}}"/>
你会添加一个这样的命令:
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);
}
}