开发者

UserControl ContextMenu binding problem

I have the following control:

<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>

The code behin:

    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;
    }

And the usage:

        <my:FilterableTreeViewControl x:Name="machineGroupTreeView" Grid.Row="0" Grid.Column="0" ItemsSource="{Binding TreeRootNodes}" FilterCaption="Filter:" SelectedItemChanged="machineGroupTreeView_SelectedItemChanged" >
            <my:FilterableTreeViewControl.ContextMenu>
                <controlsInputToolkit:Conte开发者_高级运维xtMenu>
                    <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>

But the ContextMenu does not appear. Any idea?


It is hard to answer your question because there is a lot of irrelavant stuff in your code. Try to strip your code down so that it just contains the parts that are not working!

Anyhow, I spotted something wrong in your code-behind. You have defined a dependency property where you handle its changed event. In the event handler you set the dependency property to the new value. This is totally un-necessary! The dependency property has been set to the new value at this point, you do not have to handle the change event to achieve this.


I changed the ContextMenu property to

public ObservableCollection<MenuItem> ContextMenu {
    get {
        return ( ObservableCollection<MenuItem> )GetValue( TreeContextMenuProperty );
    }
    set {
        SetValue( TreeContextMenuProperty, value );
    }
}

and everything else accordingly, and now works fine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜