TreeviewItems가 생성/표시되고 컨텍스트 메뉴가있는 DataBound 트리 뷰가 있습니다. 이것은 올바르게 작동하고 있습니다. 그런 다음 컨텍스트 메뉴에서 시작된 명령을 통해 treeviewitem의 datacontext를 검색 할 수 있도록 사용자 지정 menuitem 클래스를 사용하려고합니다. 혼란 스럽다면 죄송합니다. 나는 이것에 대해 잘못된 길을 가고있다.자식 xaml에서 treeviewitem의 datacontext를 검색합니다.
내 xaml입니다.
<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="Margin" Value="5,5,5,5" />
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu Background="{x:Static pm:myBrushes.defaultBG}" Foreground="{x:Static pm:myBrushes.gray}">
<pm:ProjectTreeMenuItem Header="Add Episode.."
Element="{Binding Path=DataContext, RelativeSource={RelativeSource AncestorType={x:Type TreeViewItem}}}"
Command="{x:Static pm:MyCommands.AddEpCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource Self}}"
Margin="20,0"/>
<pm:ProjectTreeMenuItem Header="Add Sequence.."
Element="{Binding Path=DataContext, RelativeSource={RelativeSource AncestorType={x:Type TreeViewItem}}}"
Command="{x:Static pm:MyCommands.AddSeqCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource Self}}"
Margin="20,0"/>
<pm:ProjectTreeMenuItem Header="Add Scene.."
Element="{Binding Path=DataContext, RelativeSource={RelativeSource AncestorType={x:Type TreeViewItem}}}"
Command="{x:Static pm:MyCommands.AddScCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource Self}}"
Margin="20,0"/>
</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="IsExpanded" Value="True"/>
<Setter Property="Margin" Value="5,5,5,5" />
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu Background="{x:Static pm:myBrushes.defaultBG}" Foreground="{x:Static pm:myBrushes.gray}">
<pm:ProjectTreeMenuItem Header="Add Sequence.."
Element="{Binding Path=DataContext, RelativeSource={RelativeSource AncestorType={x:Type TreeViewItem}}}"
Command="{x:Static pm:MyCommands.AddSeqCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource Self}}"
Margin="20,0"/>
<pm:ProjectTreeMenuItem Header="Add Scene.."
Element="{Binding Path=DataContext, RelativeSource={RelativeSource AncestorType={x:Type TreeViewItem}}}"
Command="{x:Static pm:MyCommands.AddScCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource Self}}"
Margin="20,0"/>
<Separator Height="15" Margin="20,0"/>
<pm:ProjectTreeMenuItem Header="Cut"
Element="{Binding Path=DataContext, RelativeSource={RelativeSource AncestorType={x:Type TreeViewItem}}}"
Command="{x:Static pm:MyCommands.CutCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource Self}}"
Margin="20,0"/>
<pm:ProjectTreeMenuItem Header="Un-Cut"
Element="{Binding Path=DataContext, RelativeSource={RelativeSource AncestorType={x:Type TreeViewItem}}}"
Command="{x:Static pm:MyCommands.UnCutCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource Self}}"
Margin="20,0"/>
</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.yellow}"
FontFamily="Calibri" FontSize="14"/>
<HierarchicalDataTemplate.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded" Value="True"/>
<Setter Property="Margin" Value="5,5,5,5" />
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu Background="{x:Static pm:myBrushes.defaultBG}" Foreground="{x:Static pm:myBrushes.gray}">
<pm:ProjectTreeMenuItem Header="Add Scene.."
Element="{Binding Path=DataContext, RelativeSource={RelativeSource AncestorType={x:Type TreeViewItem}}}"
Command="{x:Static pm:MyCommands.AddScCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource Self}}"
Margin="20,0"/>
<Separator Height="15" Margin="20,0"/>
<pm:ProjectTreeMenuItem Header="Cut"
Element="{Binding Path=DataContext, RelativeSource={RelativeSource AncestorType={x:Type TreeViewItem}}}"
Command="{x:Static pm:MyCommands.CutCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource Self}}"
Margin="20,0"/>
<pm:ProjectTreeMenuItem Header="Un-Cut"
Element="{Binding Path=DataContext, RelativeSource={RelativeSource AncestorType={x:Type TreeViewItem}}}"
Command="{x:Static pm:MyCommands.UnCutCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource Self}}"
Margin="20,0"/>
</ContextMenu>
</Setter.Value>
</Setter>
</Style>
</HierarchicalDataTemplate.ItemContainerStyle>
<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.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu Background="{x:Static pm:myBrushes.defaultBG}" Foreground="{x:Static pm:myBrushes.gray}">
<pm:ProjectTreeMenuItem Header="Cut"
Element="{Binding Path=DataContext, RelativeSource={RelativeSource AncestorType={x:Type TreeViewItem}}}"
Command="{x:Static pm:MyCommands.CutCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource Self}}"
Margin="20,0"/>
<pm:ProjectTreeMenuItem Header="Un-Cut"
Element="{Binding Path=DataContext, RelativeSource={RelativeSource AncestorType={x:Type TreeViewItem}}}"
Command="{x:Static pm:MyCommands.UnCutCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource Self}}"
Margin="20,0"/>
</ContextMenu>
</Setter.Value>
</Setter>
</Style>
</HierarchicalDataTemplate.ItemContainerStyle>
</HierarchicalDataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
</Grid>
내 사용자 정의 메뉴 항목 클래스 및 명령. (프로젝트의 다른 장소의 수백에서 사용하는 메신저)
public static class MyCommands
{
static MyCommands()
{
AddEpCommand = new SimpleDelegateCommand(p =>
{
var menuItem = p as ProjectTreeMenuItem;
var Element = menuItem.Element as ProjectElement;
Console.WriteLine(Element.Key);
Console.WriteLine("EP");
});
AddSeqCommand = new SimpleDelegateCommand(p =>
{
var menuItem = p as ProjectTreeMenuItem;
var Element = menuItem.Element as ProjectElement;
Console.WriteLine(Element.Key);
Console.WriteLine("SEQ");
});
AddScCommand = new SimpleDelegateCommand(p =>
{
var menuItem = p as ProjectTreeMenuItem;
var Element = menuItem.Element as ProjectElement;
Console.WriteLine(Element.Key);
Console.WriteLine("SC");
});
CutCommand = new SimpleDelegateCommand(p =>
{
var menuItem = p as ProjectTreeMenuItem;
var Element = menuItem.Element as ProjectElement;
Console.WriteLine(Element.Key);
Console.WriteLine("SC");
});
UnCutCommand = new SimpleDelegateCommand(p =>
{
var menuItem = p as ProjectTreeMenuItem;
var Element = menuItem.Element as ProjectElement;
Console.WriteLine(Element.Key);
Console.WriteLine("SC");
});
}
public static ICommand AddEpCommand { get; set; }
public static ICommand AddSeqCommand { get; set; }
public static ICommand AddScCommand { get; set; }
public static ICommand CutCommand { get; set; }
public static ICommand UnCutCommand { 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);
}
}
public class ProjectTreeMenuItem : MenuItem
{
#region Element (DependencyProperty)
public ProjectElement Element
{
get { return (ProjectElement)GetValue(ElementProperty); }
set { SetValue(ElementProperty, value); }
}
public static readonly DependencyProperty ElementProperty =
DependencyProperty.Register("Element", typeof(ProjectElement), typeof(ProjectTreeMenuItem),
new PropertyMetadata { PropertyChangedCallback = ElementChanged });
private static void ElementChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
ProjectTreeMenuItem so = d as ProjectTreeMenuItem;
if (so != null && e.NewValue != null)
{
so.Element = (ProjectElement)e.NewValue;
Console.WriteLine("HERE " + so.Element.Title);
}
}
#endregion
public ProjectTreeMenuItem()
:base()
{
}
}
projectElement 클래스는 다른 곳에서 잘 작동되는 문자열 키, 문자열 제목 속성이 있습니다. 임 꽤 datacontext 통해 xaml 통해 요소 속성을 설정하는 내 문제가 거짓말을 확실히.