首页 > 解决方案 > UserControl 中的 ObservableCollection DependancyProperty 中的 ContextMenu MenuItem 重复

问题描述

出于某种原因,即使我只指定了 1 个菜单项,当我重用我的用户控件时,我也有重复的上下文菜单项。

例如,测试 1 和测试 2 菜单项出现在两个用户控件中,即使它们是在单独的用户控件中编码的。

我的 MainWindow 看起来像

<Grid>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        <local:OptionsMenu Grid.Row="0" MenuWidth="23" MenuHeight="7">
            <local:OptionsMenu.MenuItems>
                <MenuItem Header="Test 1"/>
            </local:OptionsMenu.MenuItems>
        </local:OptionsMenu>

        <local:OptionsMenu Grid.Row="1" MenuWidth="23" MenuHeight="7">
            <local:OptionsMenu.MenuItems>
                <MenuItem Header="Test 2"/>
            </local:OptionsMenu.MenuItems>
        </local:OptionsMenu>
    </Grid>
</Window>

我的用户控件看起来像

<UserControl.Resources>
        <Style TargetType="{x:Type Button}" x:Key="MenuButton">

            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Canvas>
                            <Path Data="M12.5,6.5 C12.5,9.8137085 9.8137085,12.5 6.5,12.5 C3.1862915,12.5 0.5,9.8137085 0.5,6.5 C0.5,3.1862915 3.1862915,0.5 6.5,0.5 C9.8137085,0.5 12.5,3.1862915 12.5,6.5 z M30.5,6.5 C30.5,9.8137085 27.813708,12.5 24.5,12.5 C21.186292,12.5 18.5,9.8137085 18.5,6.5 C18.5,3.1862915 21.186292,0.5 24.5,0.5 C27.813708,0.5 30.5,3.1862915 30.5,6.5 z M48.5,6.5 C48.5,9.8137085 45.813708,12.5 42.5,12.5 C39.186292,12.5 36.5,9.8137085 36.5,6.5 C36.5,3.1862915 39.186292,0.5 42.5,0.5 C45.813708,0.5 48.5,3.1862915 48.5,6.5 z" 
                                    Fill="Black" 
                                    HorizontalAlignment="Left" 
                                    Stretch="Fill" 
                                    Stroke="Black"
                                    VerticalAlignment="Top" 
                                    Height="{Binding MenuHeight}" 
                                    Width="{Binding MenuWidth}" />
                            <ItemsPresenter />
                        </Canvas>

                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </UserControl.Resources>


    <Grid>
        <Button x:Name="Root" Style="{DynamicResource MenuButton}" Click="Button_Click">
            <Button.ContextMenu >
                <ContextMenu ItemsSource="{Binding MenuItems}" />
            </Button.ContextMenu>
        </Button>
    </Grid>
</UserControl>

背后的代码

public ObservableCollection<DependencyObject> MenuItems
        {
            get { return (ObservableCollection<DependencyObject>)GetValue(MenuItemsProperty); }
            set { SetValue(MenuItemsProperty, value); }
        }

        internal static readonly DependencyProperty MenuItemsProperty = DependencyProperty.Register(
            "MenuItems", typeof(ObservableCollection<DependencyObject>), typeof(OptionsMenu),
            new FrameworkPropertyMetadata(new ObservableCollection<DependencyObject>()));

标签: c#wpfxaml

解决方案


原因是您为可变引用类型的依赖属性使用了非空默认值。

您的控件的所有实例都使用相同的默认值,即相同的ObservableCollection<DependencyObject>实例,作为默认值传递

new FrameworkPropertyMetadata(new ObservableCollection<DependencyObject>())

你不能那样做。相反,null用作默认值,或者根本不设置默认值

internal static readonly DependencyProperty MenuItemsProperty = 
    DependencyProperty.Register(
        nameof(MenuItems),
        typeof(ObservableCollection<DependencyObject>),
        typeof(OptionsMenu));

并在控件的构造函数中分配一个初始值 by SetCurrentValue,与SetValue仍然允许通过 Style 和 Triggers Setters 和类似来源设置其他值相反:

public OptionsMenu()
{
    SetCurrentValue(MenuItemsProperty,
        new ObservableCollection<DependencyObject>());
}

推荐阅读