菜单切换优化

This commit is contained in:
liufei
2022-04-07 20:11:10 +08:00
parent 734b78b327
commit 5bd6af6cc7
2 changed files with 88 additions and 58 deletions

View File

@@ -22,7 +22,7 @@
</Setter> </Setter>
<Setter Property="FocusVisualStyle" Value="{x:Null}" /> <Setter Property="FocusVisualStyle" Value="{x:Null}" />
<EventSetter Event="MouseEnter" Handler="Menu_MouseEnter"/> <EventSetter Event="MouseEnter" Handler="Menu_MouseEnter"/>
<EventSetter Event="Unselected" Handler="ListBoxItem_Unselected"/> <!--<EventSetter Event="Unselected" Handler="Lbi_Unselected"/>-->
<Style.Triggers> <Style.Triggers>
<MultiTrigger> <MultiTrigger>
<MultiTrigger.Conditions> <MultiTrigger.Conditions>
@@ -50,7 +50,7 @@
</MultiTrigger> </MultiTrigger>
<!--移到代码设置-->
<!--<Trigger Property="IsSelected" Value="False"> <!--<Trigger Property="IsSelected" Value="False">
<Setter Property="Background" Value="Transparent"/> <Setter Property="Background" Value="Transparent"/>
<Setter Property="Foreground" Value="Black"/> <Setter Property="Foreground" Value="Black"/>
@@ -64,12 +64,10 @@
<Setter Property="Background" Value="Transparent"/> <Setter Property="Background" Value="Transparent"/>
<Setter Property="Foreground" Value="Black"/> <Setter Property="Foreground" Value="Black"/>
</Trigger>--> </Trigger>-->
<Trigger Property="IsMouseOver" Value="False">
<!--<MultiTrigger> <Setter Property="Background" Value="Transparent"/>
<MultiTrigger.Conditions> <Setter Property="Foreground" Value="Black"/>
<Condition Property=""></Condition> </Trigger>
</MultiTrigger.Conditions>
</MultiTrigger>-->
</Style.Triggers> </Style.Triggers>

View File

@@ -9,7 +9,7 @@ using System.Collections.ObjectModel;
using System.Threading; using System.Threading;
using System.Windows; using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input; using System.Windows.Input;
using System.Windows.Media; using System.Windows.Media;
using System.Windows.Threading; using System.Windows.Threading;
@@ -23,6 +23,8 @@ namespace GeekDesk.Control.UserControls.PannelCard
{ {
private int menuSelectIndexTemp = -1; private int menuSelectIndexTemp = -1;
private AppData appData = MainWindow.appData; private AppData appData = MainWindow.appData;
private SolidColorBrush bac = new SolidColorBrush(Color.FromRgb(236, 236, 236));
//是否正在修改菜单 //是否正在修改菜单
private static bool IS_EDIT = false; private static bool IS_EDIT = false;
@@ -30,17 +32,61 @@ namespace GeekDesk.Control.UserControls.PannelCard
public LeftCardControl() public LeftCardControl()
{ {
InitializeComponent(); 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) 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; appData.AppConfig.SelectedMenuIcons = appData.MenuList[0].IconList;
} }
else else
{ {
MenuListBox.SelectedIndex = appData.AppConfig.SelectedMenuIndex;
appData.AppConfig.SelectedMenuIcons = appData.MenuList[appData.AppConfig.SelectedMenuIndex].IconList; appData.AppConfig.SelectedMenuIcons = appData.MenuList[appData.AppConfig.SelectedMenuIndex].IconList;
} }
} }
DelegateCommand<int[]> _swap; DelegateCommand<int[]> _swap;
public DelegateCommand<int[]> SwapCommand public DelegateCommand<int[]> SwapCommand
{ {
@@ -86,9 +132,7 @@ namespace GeekDesk.Control.UserControls.PannelCard
ListBoxItem lbi = (sp.TemplatedParent as ContentPresenter).TemplatedParent as ListBoxItem; ListBoxItem lbi = (sp.TemplatedParent as ContentPresenter).TemplatedParent as ListBoxItem;
if (sp.Visibility == Visibility.Collapsed) if (sp.Visibility == Visibility.Collapsed)
{ {
SolidColorBrush scb = new SolidColorBrush(Colors.Red);
lbi.MouseEnter += Lbi_MouseEnter; lbi.MouseEnter += Lbi_MouseEnter;
if (MenuListBox.SelectedIndex != -1) if (MenuListBox.SelectedIndex != -1)
{ {
menuSelectIndexTemp = MenuListBox.SelectedIndex; menuSelectIndexTemp = MenuListBox.SelectedIndex;
@@ -101,31 +145,49 @@ namespace GeekDesk.Control.UserControls.PannelCard
} }
else else
{ {
SolidColorBrush bac = new SolidColorBrush(Color.FromRgb(236, 236, 236));
SolidColorBrush fontColor = new SolidColorBrush(Colors.Black);
lbi.MouseEnter += (s, me) => lbi.MouseEnter += (s, me) =>
{ {
lbi.Background = bac; lbi.Background = bac;
}; };
lbi.MouseLeave += Lbi_MouseLeave; lbi.MouseLeave += Lbi_MouseLeave;
lbi.Selected += Lbi_Selected;
lbi.Selected += (s, me) =>
{
lbi.MouseLeave -= Lbi_MouseLeave;
lbi.Background = bac;
lbi.Foreground = fontColor;
};
} }
} }
#region
private void Lbi_MouseEnter(object sender, MouseEventArgs e) private void Lbi_MouseEnter(object sender, MouseEventArgs e)
{ {
ListBoxItem lbi = sender as ListBoxItem; ListBoxItem lbi = sender as ListBoxItem;
lbi.Background = Brushes.Transparent; 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
/// <summary> /// <summary>
/// 新建菜单 /// 新建菜单
/// </summary> /// </summary>
@@ -135,33 +197,16 @@ namespace GeekDesk.Control.UserControls.PannelCard
{ {
MenuInfo info = new MenuInfo() { MenuEdit = Visibility.Collapsed, MenuId = System.Guid.NewGuid().ToString(), MenuName = "NewMenu" }; MenuInfo info = new MenuInfo() { MenuEdit = Visibility.Collapsed, MenuId = System.Guid.NewGuid().ToString(), MenuName = "NewMenu" };
appData.MenuList.Add(info); appData.MenuList.Add(info);
MenuListBox.Items.Refresh();
MenuListBox.SelectedIndex = appData.MenuList.Count - 1; MenuListBox.SelectedIndex = appData.MenuList.Count - 1;
appData.AppConfig.SelectedMenuIndex = MenuListBox.SelectedIndex; appData.AppConfig.SelectedMenuIndex = MenuListBox.SelectedIndex;
appData.AppConfig.SelectedMenuIcons = info.IconList; appData.AppConfig.SelectedMenuIcons = info.IconList;
//首次触发不了Selected事件
ItemCollection ic = MenuListBox.Items; object obj = MenuListBox.ItemContainerGenerator.ContainerFromIndex(MenuListBox.SelectedIndex);
SolidColorBrush bac = new SolidColorBrush(Color.FromRgb(236, 236, 236)); SetListBoxItemEvent((ListBoxItem)obj);
SolidColorBrush fontColor = new SolidColorBrush(Colors.Black); Lbi_Selected(obj, null);
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;
};
}
} }
/// <summary> /// <summary>
/// 重命名菜单 将textbox 设置为可见 /// 重命名菜单 将textbox 设置为可见
/// </summary> /// </summary>
@@ -194,7 +239,7 @@ namespace GeekDesk.Control.UserControls.PannelCard
} }
else else
{ {
index = index - 1; index--;
} }
appData.MenuList.Remove(menuInfo); 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;
}
/// <summary> /// <summary>
/// 鼠标悬停切换菜单 /// 鼠标悬停切换菜单