imil4eg
imil4eg

Reputation: 77

UserControl with ContextMenu wpf

I'm trying to create button UserControl with ContextMenu to have ability to pass specific text and image to button context. But I don't know how bind properly ContextMenu items and specify them via xaml or binding.

I tried to specify control like this with ContentPresenter inside ContextMenu block.

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

And how I used in view

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

But my menu items showed as a single element.

Also, I tried to specify IEnumerable DependencyProperty and bind ContextMenu on items but this didn't work for me. What can I do please?

Upvotes: 1

Views: 292

Answers (1)

mm8
mm8

Reputation: 169400

Adding a List read-only property to the UserControl works:

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

    ...
}

Bind to the property in the 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>

And set it like this:

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

Upvotes: 1

Related Questions