我有以下控件:
<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>
但是上下文菜单没有出现。有什么想法吗?
答案 0 :(得分:0)
很难回答你的问题,因为你的代码中有很多无关紧要的东西。尝试剥离代码,使其只包含不起作用的部分!
无论如何,我在你的代码隐藏中发现了一些错误。您已经定义了一个依赖项属性,您可以在其中处理其已更改在事件处理程序中,将依赖项属性设置为新值。这完全没必要!此时依赖项属性已设置为新值,您无需处理更改事件即可实现此目的。
答案 1 :(得分:0)
我将ContextMenu属性更改为
public ObservableCollection<MenuItem> ContextMenu {
get {
return ( ObservableCollection<MenuItem> )GetValue( TreeContextMenuProperty );
}
set {
SetValue( TreeContextMenuProperty, value );
}
}
以及其他一切相应的,现在工作正常。