Я создал свой собственный элемент управления MenuButton, который выглядит как на скриншоте ниже:
[img]https:/ /i.sstatic.net/oQSQERA4.png[/img]
Итак, пункты меню появляются, когда я нажимаю на кнопку, и скрываются, когда фокус теряется. Обработка кликов по пунктам меню также работает нормально. НО! Только со второго раза! Первый щелчок игнорируется. Я ломаю голову, пытаясь решить эту странную проблему. Может ли кто-нибудь взглянуть на мой код и сказать, где я ошибся?
public partial class MenuButton : Button, INotifyPropertyChanged
{
private readonly ContextMenu Menu;
private SolidColorBrush arrowColor = Brushes.Transparent;
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register(nameof(Text), typeof(string), typeof(MenuButton), new(string.Empty, new(OnSomeTextPropertyChanged)));
public static readonly DependencyProperty MenuPlacementProperty =
DependencyProperty.Register(nameof(MenuPlacement), typeof(PlacementMode), typeof(MenuButton), new(PlacementMode.Top, new(OnSomeMenuPlacementPropertyChanged)));
public static readonly DependencyProperty MenuItemsProperty =
DependencyProperty.Register(nameof(MenuItems), typeof(ItemCollection), typeof(MenuButton), new(default(ItemCollection), new(OnSomeMenuItemsPropertyChanged)));
public SolidColorBrush ArrowColor
{
get => arrowColor;
private set
{
arrowColor = value;
OnPropertyChanged(nameof(arrowColor));
}
}
public string Text
{
get => (string)GetValue(TextProperty);
set => SetCurrentValue(TextProperty, value);
}
public PlacementMode MenuPlacement
{
get => (PlacementMode)GetValue(MenuPlacementProperty);
set => SetCurrentValue(MenuPlacementProperty, value);
}
public ItemCollection MenuItems
{
get => (ItemCollection)GetValue(MenuItemsProperty);
set => SetCurrentValue(MenuItemsProperty, value);
}
public MenuButton()
{
Menu = new()
{
StaysOpen = true,
HasDropShadow = false,
PlacementTarget = this,
Placement = MenuPlacement,
ItemsSource = MenuItems,
};
SetCurrentValue(MenuItemsProperty, new DataGrid().Items);
InitializeComponent();
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged([CallerMemberName] string prop = "")
{
PropertyChanged?.Invoke(this, new(prop));
}
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
base.OnMouseLeftButtonDown(e);
Menu.Width = ActualWidth;
Menu.IsOpen = true;
}
private void MenuButtonControl_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if (sender is MenuButton button && bool.TryParse(e.NewValue?.ToString(), out bool enabled))
{
button.ArrowColor = enabled ? Brushes.Black : Brushes.DimGray;
}
}
private static void OnSomeTextPropertyChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
{
if (target is MenuButton menuButton)
{
menuButton.ButtonText.Content = e.NewValue.ToString();
}
}
private static void OnSomeMenuPlacementPropertyChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
{
if (target is MenuButton menuButton && menuButton.Menu is ContextMenu menu && Enum.TryParse(e.NewValue?.ToString(), true, out PlacementMode placementMode))
{
menu.Placement = placementMode;
}
}
private static void OnSomeMenuItemsPropertyChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
{
if (target is MenuButton menuButton && menuButton.Menu is ContextMenu menu && e.NewValue is ItemCollection itemCollection)
{
menu.ItemsSource = itemCollection;
menu.IsOpen = true;
menu.IsOpen = false;
}
}
}
XAML:
Подробнее здесь: https://stackoverflow.com/questions/793 ... enu-button
Кнопка раскрывающегося меню WPF ⇐ C#
Место общения программистов C#
1736056420
Anonymous
Я создал свой собственный элемент управления MenuButton, который выглядит как на скриншоте ниже:
[img]https:/ /i.sstatic.net/oQSQERA4.png[/img]
Итак, пункты меню появляются, когда я нажимаю на кнопку, и скрываются, когда фокус теряется. Обработка кликов по пунктам меню также работает нормально. [b]НО[/b]! Только со второго раза! Первый щелчок игнорируется. Я ломаю голову, пытаясь решить эту странную проблему. Может ли кто-нибудь взглянуть на мой код и сказать, где я ошибся?
public partial class MenuButton : Button, INotifyPropertyChanged
{
private readonly ContextMenu Menu;
private SolidColorBrush arrowColor = Brushes.Transparent;
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register(nameof(Text), typeof(string), typeof(MenuButton), new(string.Empty, new(OnSomeTextPropertyChanged)));
public static readonly DependencyProperty MenuPlacementProperty =
DependencyProperty.Register(nameof(MenuPlacement), typeof(PlacementMode), typeof(MenuButton), new(PlacementMode.Top, new(OnSomeMenuPlacementPropertyChanged)));
public static readonly DependencyProperty MenuItemsProperty =
DependencyProperty.Register(nameof(MenuItems), typeof(ItemCollection), typeof(MenuButton), new(default(ItemCollection), new(OnSomeMenuItemsPropertyChanged)));
public SolidColorBrush ArrowColor
{
get => arrowColor;
private set
{
arrowColor = value;
OnPropertyChanged(nameof(arrowColor));
}
}
public string Text
{
get => (string)GetValue(TextProperty);
set => SetCurrentValue(TextProperty, value);
}
public PlacementMode MenuPlacement
{
get => (PlacementMode)GetValue(MenuPlacementProperty);
set => SetCurrentValue(MenuPlacementProperty, value);
}
public ItemCollection MenuItems
{
get => (ItemCollection)GetValue(MenuItemsProperty);
set => SetCurrentValue(MenuItemsProperty, value);
}
public MenuButton()
{
Menu = new()
{
StaysOpen = true,
HasDropShadow = false,
PlacementTarget = this,
Placement = MenuPlacement,
ItemsSource = MenuItems,
};
SetCurrentValue(MenuItemsProperty, new DataGrid().Items);
InitializeComponent();
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged([CallerMemberName] string prop = "")
{
PropertyChanged?.Invoke(this, new(prop));
}
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
base.OnMouseLeftButtonDown(e);
Menu.Width = ActualWidth;
Menu.IsOpen = true;
}
private void MenuButtonControl_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if (sender is MenuButton button && bool.TryParse(e.NewValue?.ToString(), out bool enabled))
{
button.ArrowColor = enabled ? Brushes.Black : Brushes.DimGray;
}
}
private static void OnSomeTextPropertyChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
{
if (target is MenuButton menuButton)
{
menuButton.ButtonText.Content = e.NewValue.ToString();
}
}
private static void OnSomeMenuPlacementPropertyChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
{
if (target is MenuButton menuButton && menuButton.Menu is ContextMenu menu && Enum.TryParse(e.NewValue?.ToString(), true, out PlacementMode placementMode))
{
menu.Placement = placementMode;
}
}
private static void OnSomeMenuItemsPropertyChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
{
if (target is MenuButton menuButton && menuButton.Menu is ContextMenu menu && e.NewValue is ItemCollection itemCollection)
{
menu.ItemsSource = itemCollection;
menu.IsOpen = true;
menu.IsOpen = false;
}
}
}
XAML:
Подробнее здесь: [url]https://stackoverflow.com/questions/79330265/wpf-dropdown-menu-button[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия