From f73a6744002da76ad94fb6324faf37976ffe598a Mon Sep 17 00:00:00 2001 From: Peter Kirmeier Date: Thu, 11 May 2023 00:14:05 +0200 Subject: [PATCH] Change SelectionMode to Single for ListView --- Business/KeyboardInput.cs | 15 ++++++++++++--- Business/Menus.cs | 2 +- Business/WaitToLoadMenu.cs | 13 ++++++------- UserInterface/Menu.xaml | 2 +- UserInterface/Menu.xaml.cs | 26 ++++++++++++++++++++++---- 5 files changed, 42 insertions(+), 16 deletions(-) diff --git a/Business/KeyboardInput.cs b/Business/KeyboardInput.cs index f09642f..ab0f646 100644 --- a/Business/KeyboardInput.cs +++ b/Business/KeyboardInput.cs @@ -163,7 +163,13 @@ namespace SystemTrayMenu.Handler } } - internal void DeselectFoccussedRow() => focussedMenu?.GetDataGridView().SelectedItems.Remove(focussedRow); + internal void DeselectFoccussedRow() + { + if (focussedMenu != null) + { + focussedMenu.SelectedItem = null; + } + } internal void MouseSelect(Menu menu, ListViewItemData itemData) { @@ -347,7 +353,10 @@ namespace SystemTrayMenu.Handler if (doClearOldSelection) { - menuBefore?.GetDataGridView().SelectedItems.Remove(rowBefore); + if (focussedMenu != null) + { + focussedMenu.SelectedItem = null; + } } } @@ -414,7 +423,7 @@ namespace SystemTrayMenu.Handler private void Select(ListView dgv, ListViewItemData itemData) { focussedRow = itemData; - dgv.SelectedItems.Add(itemData); + dgv.SelectedItem = itemData; } } } diff --git a/Business/Menus.cs b/Business/Menus.cs index b6f5ba6..3f4ab06 100644 --- a/Business/Menus.cs +++ b/Business/Menus.cs @@ -390,7 +390,7 @@ namespace SystemTrayMenu.Business Menu? menu = mainMenu; if (menu != null) { - menu.GetDataGridView().SelectedItem = null; + menu.SelectedItem = null; menu.RelocateOnNextShow = true; diff --git a/Business/WaitToLoadMenu.cs b/Business/WaitToLoadMenu.cs index f033f92..19b0e0b 100644 --- a/Business/WaitToLoadMenu.cs +++ b/Business/WaitToLoadMenu.cs @@ -7,7 +7,6 @@ namespace SystemTrayMenu.Handler using System; using System.Windows.Threading; using SystemTrayMenu.DataClasses; - using SystemTrayMenu.Utilities; using static SystemTrayMenu.UserInterface.Menu; using ListView = System.Windows.Controls.ListView; using Menu = SystemTrayMenu.UserInterface.Menu; @@ -64,13 +63,13 @@ namespace SystemTrayMenu.Handler timerStartLoad.Start(); } - internal void MouseLeave(ListView dgv) + internal void MouseLeave(Menu menu) { if (MouseActive) { timerStartLoad.Stop(); StopLoadMenu?.Invoke(); - ResetData(dgv); + ResetData(menu); } } @@ -78,7 +77,7 @@ namespace SystemTrayMenu.Handler { timerStartLoad.Stop(); StopLoadMenu?.Invoke(); - ResetData(menu.GetDataGridView()); + ResetData(menu); MouseActive = false; } @@ -141,16 +140,16 @@ namespace SystemTrayMenu.Handler { alreadyOpened = false; - menu.GetDataGridView().SelectedItem = currentItemData = itemData; + menu.SelectedItem = currentItemData = itemData; currentMenu = menu; } } - private void ResetData(ListView dgv) + private void ResetData(Menu menu) { if (currentMenu != null) { - dgv.SelectedItem = currentItemData = null; + menu.SelectedItem = currentItemData = null; currentMenu = null; } } diff --git a/UserInterface/Menu.xaml b/UserInterface/Menu.xaml index d0c1a0e..0864681 100644 --- a/UserInterface/Menu.xaml +++ b/UserInterface/Menu.xaml @@ -171,7 +171,7 @@ - diff --git a/UserInterface/Menu.xaml.cs b/UserInterface/Menu.xaml.cs index d482874..41e27f9 100644 --- a/UserInterface/Menu.xaml.cs +++ b/UserInterface/Menu.xaml.cs @@ -237,7 +237,7 @@ namespace SystemTrayMenu.UserInterface internal event Action? CellMouseEnter; - internal event Action? CellMouseLeave; + internal event Action? CellMouseLeave; internal event Action? CellMouseDown; @@ -278,6 +278,12 @@ namespace SystemTrayMenu.UserInterface internal RowData? RowDataParent { get; set; } + internal ListViewItemData? SelectedItem + { + get => (ListViewItemData?)dgv.SelectedItem; + set => dgv.SelectedItem = value; + } + internal Menu MainMenu { get; init; } internal Menu? ParentMenu => RowDataParent?.Owner; @@ -1200,7 +1206,7 @@ namespace SystemTrayMenu.UserInterface ListView lv = (ListView)sender; foreach (ListViewItemData itemData in lv.Items) { - itemData.IsSelected = lv.SelectedItems.Contains(itemData); + itemData.IsSelected = lv.SelectedItem == itemData; itemData.UpdateColors(); } } @@ -1209,7 +1215,7 @@ namespace SystemTrayMenu.UserInterface private void ListViewItem_MouseEnter(object sender, MouseEventArgs e) => CellMouseEnter?.Invoke(this, (ListViewItemData)((ListViewItem)sender).Content); - private void ListViewItem_MouseLeave(object sender, MouseEventArgs e) => CellMouseLeave?.Invoke(dgv); + private void ListViewItem_MouseLeave(object sender, MouseEventArgs e) => CellMouseLeave?.Invoke(this); private void ListViewItem_PreviewMouseDown(object sender, MouseButtonEventArgs e) { @@ -1250,6 +1256,7 @@ namespace SystemTrayMenu.UserInterface private Brush? backgroundBrush; private Brush? borderBrush; private ImageSource? columnIcon; + private bool isSelected; internal ListViewItemData(ImageSource? columnIcon, string columnText, RowData rowData, int sortIndex) { @@ -1310,7 +1317,18 @@ namespace SystemTrayMenu.UserInterface internal bool IsPendingOpenItem { get; set; } - internal bool IsSelected { get; set; } + internal bool IsSelected + { + get => isSelected; + set + { + if (value != isSelected) + { + isSelected = value; + CallPropertyChanged(); + } + } + } public override string ToString() => nameof(ListViewItemData) + ": " + ColumnText + ", Owner: " + (data.Owner?.ToString() ?? "null");