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

96 lines
2.7 KiB
C#
Raw Normal View History

2022-01-11 19:42:12 +01:00
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
// <20> 2022 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
2022-01-13 20:12:49 +01:00
using System.Collections.ObjectModel;
using System.Collections.Specialized;
2021-12-05 17:16:14 +01:00
using System.Linq;
#if WINUI
using Microsoft.UI.Xaml;
2022-01-13 20:12:49 +01:00
using Microsoft.UI.Xaml.Markup;
2021-12-05 17:16:14 +01:00
#elif UWP
using Windows.UI.Xaml;
2022-01-13 20:12:49 +01:00
using Windows.UI.Xaml.Markup;
2021-12-05 17:16:14 +01:00
#else
using System.Windows;
2022-01-13 20:12:49 +01:00
using System.Windows.Markup;
2021-12-05 17:16:14 +01:00
#endif
2022-01-11 19:42:12 +01:00
namespace MapControl.UiTools
2021-12-05 17:16:14 +01:00
{
2022-01-13 20:12:49 +01:00
#if WINUI || UWP
[ContentProperty(Name = nameof(Projection))]
#else
[ContentProperty(nameof(Projection))]
#endif
public class MapProjectionItem
{
public string Text { get; set; }
public MapProjection Projection { get; set; }
}
#if WINUI || UWP
[ContentProperty(Name = nameof(MapProjections))]
#else
[ContentProperty(nameof(MapProjections))]
#endif
2021-12-05 17:16:14 +01:00
public class MapProjectionsMenuButton : MenuButton
{
public MapProjectionsMenuButton()
2022-01-11 19:42:12 +01:00
: base("\uE809")
2021-12-05 17:16:14 +01:00
{
2022-01-13 20:12:49 +01:00
((INotifyCollectionChanged)MapProjections).CollectionChanged += (s, e) => InitializeMenu();
2021-12-05 17:16:14 +01:00
}
public static readonly DependencyProperty MapProperty = DependencyProperty.Register(
nameof(Map), typeof(MapBase), typeof(MapProjectionsMenuButton),
new PropertyMetadata(null, (o, e) => ((MapProjectionsMenuButton)o).InitializeMenu()));
public MapBase Map
{
get { return (MapBase)GetValue(MapProperty); }
set { SetValue(MapProperty, value); }
}
2022-01-13 20:12:49 +01:00
public Collection<MapProjectionItem> MapProjections { get; } = new ObservableCollection<MapProjectionItem>();
2021-12-05 17:16:14 +01:00
private void InitializeMenu()
{
2022-01-13 20:12:49 +01:00
if (Map != null)
2021-12-05 17:16:14 +01:00
{
var menu = CreateMenu();
2022-01-13 20:12:49 +01:00
foreach (var item in MapProjections)
2021-12-05 17:16:14 +01:00
{
2022-01-13 20:12:49 +01:00
menu.Items.Add(CreateMenuItem(item.Text, item.Projection, MapProjectionClicked));
2021-12-05 17:16:14 +01:00
}
2022-01-13 20:12:49 +01:00
var initialProjection = MapProjections.Select(p => p.Projection).FirstOrDefault();
2021-12-05 17:16:14 +01:00
if (initialProjection != null)
{
SetMapProjection(initialProjection);
}
}
}
private void MapProjectionClicked(object sender, RoutedEventArgs e)
{
var item = (FrameworkElement)sender;
var projection = (MapProjection)item.Tag;
SetMapProjection(projection);
}
private void SetMapProjection(MapProjection projection)
{
Map.MapProjection = projection;
foreach (var item in GetMenuItems())
{
item.IsChecked = Map.MapProjection == (MapProjection)item.Tag;
}
}
}
}