emby-toolbox/EmbyToolbox/Behaviors/ContextMenuOpenOnButtonClickBehavior.cs
Emby Toolbox 6264b487fe Initial commit: Emby Toolbox (conversion scroll fix, bulk Del for tracks).
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-12 21:33:47 +05:00

48 lines
1.4 KiB
C#

using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
namespace EmbyToolbox.Behaviors;
/// <summary>Всплывает <see cref="Button.ContextMenu"/> по левому клику вместо ПКМ (split-menu кнопка).</summary>
public static class ContextMenuOpenOnButtonClickBehavior
{
public static readonly DependencyProperty IsEnabledProperty = DependencyProperty.RegisterAttached(
"IsEnabled",
typeof(bool),
typeof(ContextMenuOpenOnButtonClickBehavior),
new PropertyMetadata(false, OnIsEnabledChanged));
public static void SetIsEnabled(Button obj, bool value) => obj.SetValue(IsEnabledProperty, value);
public static bool GetIsEnabled(Button obj) => (bool)obj.GetValue(IsEnabledProperty);
private static void OnIsEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is not Button btn)
{
return;
}
btn.Click -= OnButtonClick;
if (e.NewValue is true)
{
btn.Click += OnButtonClick;
}
}
private static void OnButtonClick(object sender, RoutedEventArgs e)
{
if (sender is not Button btn || btn.ContextMenu is not ContextMenu menu)
{
return;
}
menu.PlacementTarget = btn;
menu.Placement = PlacementMode.Bottom;
menu.IsOpen = true;
e.Handled = true;
}
}