Multiprocotol Terminalprogram (BAT)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
MultiTerm/MultiTerm.Wpf/Controls/SingleSelectSubMenu.cs

124 lines
4.1 KiB

using MultiTerm.Wpf.ValueConverters;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;
using System;
using System.Printing;
namespace MultiTerm.Wpf.Controls;
public class SingleSelectSubMenu : MenuItem
{
public static Dictionary<MenuItem, SingleSelectSubMenu> RegisteredSubItemsAndParent = new Dictionary<MenuItem, SingleSelectSubMenu>();
public static readonly DependencyProperty OptionsSourceProperty =
DependencyProperty.Register("OptionsSource",
typeof(IEnumerable),
typeof(SingleSelectSubMenu),
new PropertyMetadata(null, OnOptionsSourceChanged));
public static readonly DependencyProperty SelectedMenuItemProperty =
DependencyProperty.Register("SelectedMenuItem",
typeof(object),
typeof(SingleSelectSubMenu),
new PropertyMetadata(null, OnSelectedMenuItemPropertyChanged));
private static void OnSelectedMenuItemPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var sssm = d as SingleSelectSubMenu;
if (sssm != null)
{
if(e.NewValue != null)
{
// get menu item with same name from registered items
MenuItem selectedMenuItem = null;
foreach (var item in RegisteredSubItemsAndParent)
{
if(String.Compare(item.Key.Header.ToString(), e.NewValue.ToString(), true) == 0)
{
selectedMenuItem = item.Key;
break;
}
}
// var selectedMenuItem = RegisteredSubItemsAndParent.Where(x => x.Key.Header == e.NewValue).FirstOrDefault();
if(selectedMenuItem != null)
{
selectedMenuItem.IsChecked = true;
//OnAnyItemChecked(selectedMenuItem, new RoutedEventArgs());
}
}
}
}
private static void OnOptionsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var sssm = d as SingleSelectSubMenu;
if(sssm != null)
{
if(e.NewValue != null)
{
var parent = sssm.Parent as MenuItem;
if(parent != null)
{
foreach (var item in (IEnumerable)e.NewValue)
{
var converter = new EnumDescriptionConverter();
var newItem = new MenuItem()
{
Header = converter.Convert(item, typeof(object), null, CultureInfo.CurrentCulture),
IsCheckable = true
};
newItem.Checked += OnAnyItemChecked;
RegisteredSubItemsAndParent.Add(newItem, sssm);
parent.Items.Add(newItem);
Debug.WriteLine(item);
}
}
}
}
}
private static void OnAnyItemChecked(object sender, RoutedEventArgs e)
{
var menuItem = sender as MenuItem;
if(menuItem != null)
{
Debug.WriteLine($"menuitem checked: {menuItem}");
// uncheck others
foreach (var item in RegisteredSubItemsAndParent)
{
if (item.Key != null && item.Key != menuItem)
{
item.Key.IsChecked = false;
}
}
// set menu item for respective parent
RegisteredSubItemsAndParent[menuItem].SelectedMenuItem = menuItem;
}
}
[Bindable(true)]
public IEnumerable OptionsSource
{
get { return(IEnumerable)GetValue(OptionsSourceProperty); }
set { SetValue(OptionsSourceProperty, value); }
}
[Bindable(true)]
public object SelectedMenuItem
{
get { return GetValue(SelectedMenuItemProperty); }
set { SetValue (SelectedMenuItemProperty, value); }
}
}