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

107 lines
3 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; }
2022-01-19 16:43:00 +01:00
public string Projection { get; set; }
2022-01-13 20:12:49 +01:00
}
#if WINUI || UWP
[ContentProperty(Name = nameof(MapProjections))]
#else
[ContentProperty(nameof(MapProjections))]
#endif
2021-12-05 17:16:14 +01:00
public class MapProjectionsMenuButton : MenuButton
{
2022-01-21 18:37:05 +01:00
private string selectedProjection;
2021-12-05 17:16:14 +01:00
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
{
2022-08-06 11:04:49 +02:00
get => (MapBase)GetValue(MapProperty);
set => SetValue(MapProperty, value);
2021-12-05 17:16:14 +01:00
}
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;
2022-01-19 16:43:00 +01:00
var projection = (string)item.Tag;
2021-12-05 17:16:14 +01:00
SetMapProjection(projection);
}
2022-01-19 16:43:00 +01:00
private void SetMapProjection(string projection)
2021-12-05 17:16:14 +01:00
{
2022-01-21 18:37:05 +01:00
if (selectedProjection != projection)
{
selectedProjection = projection;
2022-01-22 20:32:17 +01:00
Map.MapProjection = MapProjection.Factory.GetProjection(selectedProjection);
2022-01-21 18:37:05 +01:00
}
2021-12-05 17:16:14 +01:00
2022-01-21 18:37:05 +01:00
UpdateCheckedStates();
}
private void UpdateCheckedStates()
{
2021-12-05 17:16:14 +01:00
foreach (var item in GetMenuItems())
{
2022-01-21 18:37:05 +01:00
item.IsChecked = selectedProjection == (string)item.Tag;
2021-12-05 17:16:14 +01:00
}
}
}
}