У меня есть следующий элемент управления:
<UserControl x:Class="DNC_v3_0_Admin.Controls.FilterableTreeViewControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:Controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
xmlns:Converters="clr-namespace:DNC_v3_0_Admin.Converters"
xmlns:Data="clr-namespace:System.Windows;assembly=System.Windows.Controls"
xmlns:controlsInputToolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400" xmlns:my="clr-namespace:DNC_v3_0_Admin.Controls"
x:Name="MyFilterableTreeViewControl">
<UserControl.Resources>
<ImageBrush x:Key="PropertiesBrush" ImageSource="../Resources/machine.png"/>
<Converters:ManagedObjectNodeIconConverter x:Key="TreeIconConverter"/>
<Data:HierarchicalDataTemplate x:Key="FilterableTreeViewTemplate" ItemsSource="{Binding Children}">
<StackPanel Orientation="Horizontal" Height="Auto" Width="Auto" Grid.Row="0">
<Image Source="{Binding NodeType,Converter={StaticResource TreeIconConverter}}"/>
<TextBlock x:Name="NameTextBlock" Text="{Binding Name}" controlsInputToolkit:ContextMenuService.ContextMenu="{Binding ElementName=MyFilterableTreeViewControl, Path=ContextMenu, Mode=TwoWay}" />
</StackPanel>
</Data:HierarchicalDataTemplate>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White">
<Controls:TreeView Name="treeView" Style="{StaticResource MachineGroupStyle}" ItemsSource="{Binding ElementName=MyFilterableTreeViewControl, Path=ItemsSource}" SelectedItemChanged="treeView_SelectedItemChanged" />
</Grid>
</UserControl>
Код здесь:
public ContextMenu ContextMenu {
get {
return ( ContextMenu )GetValue( ContextMenuProperty );
}
set {
SetValue( ContextMenuProperty, value );
}
}
public static readonly DependencyProperty ContextMenuProperty =
DependencyProperty.Register( "ContextMenu", typeof( ContextMenu ), typeof( FilterableTreeViewControl ),
new PropertyMetadata( null, new PropertyChangedCallback( FilterableTreeViewControl.OnContextMenuPropertyChange ) ) );
private static void OnContextMenuPropertyChange( DependencyObject d, DependencyPropertyChangedEventArgs e ) {
FilterableTreeViewControl ctrl = d as FilterableTreeViewControl;
ctrl.OnContextMenuChange( ( ContextMenu )e.NewValue );
}
protected virtual void OnContextMenuChange( ContextMenu NewContextMenu ) {
//ContextMenu = NewContextMenu;
}
И использование:
<my:FilterableTreeViewControl x:Name="machineGroupTreeView" Grid.Row="0" Grid.Column="0" ItemsSource="{Binding TreeRootNodes}" FilterCaption="Filter:" SelectedItemChanged="machineGroupTreeView_SelectedItemChanged" >
<my:FilterableTreeViewControl.ContextMenu>
<controlsInputToolkit:ContextMenu>
<controlsInputToolkit:MenuItem Header="New" Command="{Binding NewCommand}" CommandParameter="{Binding Id}" >
<controlsInputToolkit:MenuItem.Icon>
<Rectangle Width="16" Height="16" Fill="{StaticResource PropertiesBrush}"/>
</controlsInputToolkit:MenuItem.Icon>
</controlsInputToolkit:MenuItem>
</controlsInputToolkit:ContextMenu>
</my:FilterableTreeViewControl.ContextMenu>
</my:FilterableTreeViewControl>
Но ContextMenu не появляется,Есть идеи?