带上下文菜单的用户控件 wpf

UserControl with ContextMenu wpf

我正在尝试使用 ContextMenu 创建按钮 UserControl,以便能够将特定文本和图像传递到按钮上下文。但我不知道如何正确绑定 ContextMenu 项并通过 xaml 或绑定指定它们。

我尝试在 ContextMenu 块中使用 ContentPresenter 指定这样的控件。

<UserControl.Template>
   <ControlTemplate TargetType="UserControl">
      <Button Style="{StaticResource HeaderButton}" app:ContextMenuLeftClickBehavior.IsLeftClickEnabled="True">
         <Button.Content>
            <StackPanel Orientation="Horizontal">
               <Image Source="{Binding ImageSource, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:MenuControl}}}" />
               <TextBlock Text="{Binding Text, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:MenuControl}}}" />
               <Path Stroke="Black"
                  StrokeThickness="1"
                  Margin="5 0 5 0"
                  Data="M 0 4 L 5 10 M 5 10 L 10 4" />
            </StackPanel>
         </Button.Content>
         <Button.ContextMenu>
            <ContextMenu>
               <ContentPresenter />
            </ContextMenu>
         </Button.ContextMenu>
      </Button>
   </ControlTemplate>
</UserControl.Template>

以及我在视图中的使用方式

<control:MenuControl ImageSource="Icons/TestPlan.png" Text="Load">
   <StackPanel>
      <MenuItem Header="test1" />
      <MenuItem Header="test2" />
   </StackPanel>
</control:MenuControl>

但我的菜单项显示为单个元素。

此外,我尝试指定 IEnumerable DependencyProperty 并在项目上绑定 ContextMenu,但这对我不起作用。请问我能做什么?

将列表 read-only 属性 添加到 UserControl 作品:

public partial class MenuControl : UserControl
{
    public MenuControl()
    {
        InitializeComponent();
        SetValue(ItemsPropertyKey, new List<MenuItem>());
    }

    private static readonly DependencyPropertyKey ItemsPropertyKey = DependencyProperty.RegisterReadOnly(
      nameof(Items),
      typeof(List<MenuItem>),
      typeof(MenuControl),
      new FrameworkPropertyMetadata(null)
    );

    public static readonly DependencyProperty ItemsProperty = ItemsPropertyKey.DependencyProperty;

    public List<MenuItem> Items
    {
        get { return (List<MenuItem>)GetValue(ItemsProperty); }
    }

    ...
}

绑定到 ControlTemplate 中的 属性:

<ControlTemplate TargetType="UserControl">
    <Button>
        <Button.Content>
            <StackPanel Orientation="Horizontal">
                <Image Source="{Binding ImageSource, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:MenuControl}}}" />
                <TextBlock Text="{Binding Text, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:MenuControl}}}" />
                <Path Stroke="Black"
                      StrokeThickness="1"
                      Margin="5 0 5 0"
                      Data="M 0 4 L 5 10 M 5 10 L 10 4" />
            </StackPanel>
        </Button.Content>
        <Button.Tag>
            <Binding RelativeSource="{RelativeSource AncestorType={x:Type local:MenuControl}}" />
        </Button.Tag>
        <Button.ContextMenu>
            <ContextMenu ItemsSource="{Binding PlacementTarget.Tag.Items, RelativeSource={RelativeSource Self}}" />
        </Button.ContextMenu>
    </Button>
</ControlTemplate>

然后这样设置:

<control:MenuControl x:Name="cc" ImageSource="Icons/TestPlan.png" Text="Load">
    <control:MenuControl.Items>
        <MenuItem Header="test1" />
        <MenuItem Header="test2" />
    </control:MenuControl.Items>
</control:MenuControl>