XAML-Map-Control/MapUiTools/Shared/MenuButton.cs

50 lines
1.2 KiB
C#
Raw Normal View History

2025-09-13 09:10:41 +02:00
using System.Linq;
using System.Threading.Tasks;
#if WPF
using System.Windows;
using System.Windows.Controls;
#elif UWP
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
#elif WINUI
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
2026-04-10 16:55:33 +02:00
#elif AVALONIA
using Avalonia.Controls;
using DependencyProperty = Avalonia.AvaloniaProperty;
#endif
2026-04-13 17:14:49 +02:00
namespace MapControl.UiTools;
public partial class MenuButton : Button
{
2026-04-13 17:14:49 +02:00
public static readonly DependencyProperty MapProperty =
DependencyPropertyHelper.Register<MenuButton, MapBase>(nameof(Map), null,
async (button, oldValue, newValue) => await button.Initialize());
2026-04-13 17:14:49 +02:00
public MapBase Map
{
get => (MapBase)GetValue(MapProperty);
set => SetValue(MapProperty, value);
}
2026-04-13 17:14:49 +02:00
private async Task Initialize()
{
if (Map != null)
{
2026-04-13 17:14:49 +02:00
DataContext = Map;
2026-04-13 17:14:49 +02:00
var initialItem =
Items.OfType<MapMenuItem>().FirstOrDefault(item => item.IsChecked) ??
Items.OfType<MapMenuItem>().FirstOrDefault();
2025-09-13 09:10:41 +02:00
2026-04-13 17:14:49 +02:00
if (initialItem != null)
{
initialItem.IsChecked = true;
2026-04-13 17:14:49 +02:00
await initialItem.ExecuteAsync(Map);
}
}
}
}