From 5bd6af6cc7d8338064789b6460921d2b661c18c3 Mon Sep 17 00:00:00 2001 From: liufei Date: Thu, 7 Apr 2022 20:11:10 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8F=9C=E5=8D=95=E5=88=87=E6=8D=A2=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../PannelCard/LeftCardControl.xaml | 14 +- .../PannelCard/LeftCardControl.xaml.cs | 132 +++++++++++------- 2 files changed, 88 insertions(+), 58 deletions(-) diff --git a/Control/UserControls/PannelCard/LeftCardControl.xaml b/Control/UserControls/PannelCard/LeftCardControl.xaml index d0c6484..e315888 100644 --- a/Control/UserControls/PannelCard/LeftCardControl.xaml +++ b/Control/UserControls/PannelCard/LeftCardControl.xaml @@ -22,7 +22,7 @@ - + @@ -50,7 +50,7 @@ - + - - + + + + diff --git a/Control/UserControls/PannelCard/LeftCardControl.xaml.cs b/Control/UserControls/PannelCard/LeftCardControl.xaml.cs index a0692e5..3150af2 100644 --- a/Control/UserControls/PannelCard/LeftCardControl.xaml.cs +++ b/Control/UserControls/PannelCard/LeftCardControl.xaml.cs @@ -9,7 +9,7 @@ using System.Collections.ObjectModel; using System.Threading; using System.Windows; using System.Windows.Controls; - +using System.Windows.Data; using System.Windows.Input; using System.Windows.Media; using System.Windows.Threading; @@ -23,6 +23,8 @@ namespace GeekDesk.Control.UserControls.PannelCard { private int menuSelectIndexTemp = -1; private AppData appData = MainWindow.appData; + private SolidColorBrush bac = new SolidColorBrush(Color.FromRgb(236, 236, 236)); + //是否正在修改菜单 private static bool IS_EDIT = false; @@ -30,17 +32,61 @@ namespace GeekDesk.Control.UserControls.PannelCard public LeftCardControl() { InitializeComponent(); + + + this.Loaded += (s, e) => + { + SelectLastMenu(); + SetMenuListBoxItemEvent(); + }; + } + + + private void SetMenuListBoxItemEvent() + { + int size = MenuListBox.Items.Count; + for (int i = 0; i < size; i++) + { + ListBoxItem lbi = (ListBoxItem)(MenuListBox.ItemContainerGenerator.ContainerFromIndex(i)); + if (lbi != null) + { + SetListBoxItemEvent(lbi); + } + } + //首次触发不了Selected事件 + object obj = MenuListBox.ItemContainerGenerator.ContainerFromIndex(MenuListBox.SelectedIndex); + Lbi_Selected(obj, null); + } + + private void SetListBoxItemEvent(ListBoxItem lbi) + { + lbi.MouseEnter += (s, me) => + { + lbi.Background = bac; + }; + lbi.Unselected += Lbi_Unselected; + + lbi.MouseLeave += Lbi_MouseLeave; + + lbi.Selected += Lbi_Selected; + } + + private void SelectLastMenu() + { if (appData.AppConfig.SelectedMenuIndex >= appData.MenuList.Count || appData.AppConfig.SelectedMenuIndex == -1) { + MenuListBox.SelectedIndex = 0; + appData.AppConfig.SelectedMenuIndex = MenuListBox.SelectedIndex; appData.AppConfig.SelectedMenuIcons = appData.MenuList[0].IconList; } else { + MenuListBox.SelectedIndex = appData.AppConfig.SelectedMenuIndex; appData.AppConfig.SelectedMenuIcons = appData.MenuList[appData.AppConfig.SelectedMenuIndex].IconList; } } - DelegateCommand _swap; + DelegateCommand _swap; public DelegateCommand SwapCommand { @@ -86,9 +132,7 @@ namespace GeekDesk.Control.UserControls.PannelCard ListBoxItem lbi = (sp.TemplatedParent as ContentPresenter).TemplatedParent as ListBoxItem; if (sp.Visibility == Visibility.Collapsed) { - SolidColorBrush scb = new SolidColorBrush(Colors.Red); lbi.MouseEnter += Lbi_MouseEnter; - if (MenuListBox.SelectedIndex != -1) { menuSelectIndexTemp = MenuListBox.SelectedIndex; @@ -101,31 +145,49 @@ namespace GeekDesk.Control.UserControls.PannelCard } else { - SolidColorBrush bac = new SolidColorBrush(Color.FromRgb(236, 236, 236)); - SolidColorBrush fontColor = new SolidColorBrush(Colors.Black); - lbi.MouseEnter += (s, me) => { lbi.Background = bac; }; lbi.MouseLeave += Lbi_MouseLeave; - - lbi.Selected += (s, me) => - { - lbi.MouseLeave -= Lbi_MouseLeave; - lbi.Background = bac; - lbi.Foreground = fontColor; - }; + lbi.Selected += Lbi_Selected; } } + #region 设置菜单触发事件 private void Lbi_MouseEnter(object sender, MouseEventArgs e) { ListBoxItem lbi = sender as ListBoxItem; lbi.Background = Brushes.Transparent; } + private void Lbi_Unselected(object sender, RoutedEventArgs e) + { + //添加Leave效果 + ListBoxItem lbi = sender as ListBoxItem; + lbi.Background = Brushes.Transparent; + lbi.MouseLeave += Lbi_MouseLeave; + } + + private void Lbi_Selected(object sender, RoutedEventArgs e) + { + ListBoxItem lbi = sender as ListBoxItem; + + SolidColorBrush fontColor = new SolidColorBrush(Colors.Black); + + lbi.MouseLeave -= Lbi_MouseLeave; + lbi.Background = bac; + lbi.Foreground = fontColor; + } + + private void Lbi_MouseLeave(object sender, MouseEventArgs e) + { + ListBoxItem lbi = sender as ListBoxItem; + lbi.Background = Brushes.Transparent; + } + #endregion + /// /// 新建菜单 /// @@ -135,33 +197,16 @@ namespace GeekDesk.Control.UserControls.PannelCard { MenuInfo info = new MenuInfo() { MenuEdit = Visibility.Collapsed, MenuId = System.Guid.NewGuid().ToString(), MenuName = "NewMenu" }; appData.MenuList.Add(info); - MenuListBox.Items.Refresh(); MenuListBox.SelectedIndex = appData.MenuList.Count - 1; appData.AppConfig.SelectedMenuIndex = MenuListBox.SelectedIndex; appData.AppConfig.SelectedMenuIcons = info.IconList; - - ItemCollection ic = MenuListBox.Items; - SolidColorBrush bac = new SolidColorBrush(Color.FromRgb(236, 236, 236)); - SolidColorBrush fontColor = new SolidColorBrush(Colors.Black); - foreach (var icItem in ic) - { - ListBoxItem lbi = icItem as ListBoxItem; - lbi.MouseEnter += (s, me) => - { - lbi.Background = bac; - }; - - lbi.MouseLeave += Lbi_MouseLeave; - - lbi.Selected += (s, me) => - { - lbi.MouseLeave -= Lbi_MouseLeave; - lbi.Background = bac; - lbi.Foreground = fontColor; - }; - } + //首次触发不了Selected事件 + object obj = MenuListBox.ItemContainerGenerator.ContainerFromIndex(MenuListBox.SelectedIndex); + SetListBoxItemEvent((ListBoxItem)obj); + Lbi_Selected(obj, null); } + /// /// 重命名菜单 将textbox 设置为可见 /// @@ -194,7 +239,7 @@ namespace GeekDesk.Control.UserControls.PannelCard } else { - index = index - 1; + index--; } appData.MenuList.Remove(menuInfo); @@ -284,19 +329,6 @@ namespace GeekDesk.Control.UserControls.PannelCard } } - private void ListBoxItem_Unselected(object sender, RoutedEventArgs e) - { - //添加Leave效果 - ListBoxItem lbi = sender as ListBoxItem; - lbi.Background = Brushes.Transparent; - lbi.MouseLeave += Lbi_MouseLeave; - } - - private void Lbi_MouseLeave(object sender, MouseEventArgs e) - { - ListBoxItem lbi = sender as ListBoxItem; - lbi.Background = Brushes.Transparent; - } /// /// 鼠标悬停切换菜单