ContextMenu под кнопкой левой кнопкой мыши - PullRequest
0 голосов
/ 31 марта 2020

Я пытаюсь сделать кнопку с меню, которое открывается левой кнопкой мыши. Проблема в том, что положение contextMenu находится на точке мыши, но мне нужно как this .

ContextMenuService.Placement="Bottom" работает только для щелчка правой кнопкой мыши

<Button ContextMenuService.IsEnabled="false" ContextMenuService.Placement="Bottom" Content="123" > 
    <Button.Resources>
        <helpers:BindingProxy x:Key="proxy" Data="{Binding}" />
    </Button.Resources>
    <Button.ContextMenu>
        <ContextMenu IsOpen="{Binding Path=Data.IsOpenCMenu, Mode=TwoWay, Source={StaticResource proxy}, UpdateSourceTrigger=PropertyChanged}" >
            <MenuItem Header="Menu item 1" />
            <MenuItem Header="Menu item 2" />
            <Separator />
            <MenuItem Header="Menu item 3" />
        </ContextMenu>
    </Button.ContextMenu>
</Button>

1 Ответ

1 голос
/ 01 апреля 2020

Самый простой способ сделать это - использовать поведение. Это позволит вам использовать его в XAML без какого-либо кода. Вы также можете использовать это поведение в другом месте.

XAML

<Button
    Width="100"
    HorizontalAlignment="Center"
    VerticalAlignment="Center"
    Content="123">
    <i:Interaction.Behaviors>
        <local:ButtonContextMenuLeftClickBehavior />
    </i:Interaction.Behaviors>
    <Button.ContextMenu>
        <ContextMenu>
            <MenuItem Header="Menu item 1" />
            <MenuItem Header="Menu item 2" />
            <Separator />
            <MenuItem Header="Menu item 3" />
        </ContextMenu>
    </Button.ContextMenu>
</Button>

Поведение

using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Interactivity;

namespace SO
{
    public class ButtonContextMenuLeftClickBehavior : Behavior<Button>
    {
        public ButtonContextMenuLeftClickBehavior()
        {
        }

        protected override void OnAttached()
        {
            base.OnAttached();
            AssociatedObject.Loaded += OnLoaded;
        }

        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            AssociatedObject.Click += OnClick;
        }

        private void OnClick(object sender, RoutedEventArgs e)
        {
            if (!(sender is Button button) || button.ContextMenu == null) return;

            button.ContextMenu.PlacementTarget = button;
            button.ContextMenu.Placement = PlacementMode.Bottom;

            if (button.ContextMenu.DataContext == null)
            {
                button.ContextMenu.SetBinding(FrameworkElement.DataContextProperty, new Binding {Source = button.DataContext});
            }

            button.ContextMenu.IsOpen = true;
            button.ContextMenu.Closed += (_, __) => { button.IsEnabled = true; };
            button.IsEnabled = false;
        }
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...