悬停切换菜单

This commit is contained in:
liufei
2022-03-25 17:57:16 +08:00
parent b0066939dd
commit 701dc27361
2 changed files with 194 additions and 61 deletions

View File

@@ -21,8 +21,8 @@
</Setter.Value> </Setter.Value>
</Setter> </Setter>
<Setter Property="FocusVisualStyle" Value="{x:Null}" /> <Setter Property="FocusVisualStyle" Value="{x:Null}" />
<EventSetter Event="MouseLeftButtonDown" Handler="MenuClick"/> <EventSetter Event="MouseEnter" Handler="Menu_MouseEnter"/>
<EventSetter Event="MouseRightButtonDown" Handler="MenuClick"/> <EventSetter Event="Unselected" Handler="ListBoxItem_Unselected"/>
<Style.Triggers> <Style.Triggers>
<MultiTrigger> <MultiTrigger>
<MultiTrigger.Conditions> <MultiTrigger.Conditions>
@@ -43,11 +43,34 @@
</Storyboard> </Storyboard>
</BeginStoryboard> </BeginStoryboard>
</MultiTrigger.ExitActions> </MultiTrigger.ExitActions>
<MultiTrigger.Setters>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Foreground" Value="Black"/>
</MultiTrigger.Setters>
</MultiTrigger> </MultiTrigger>
<Trigger Property="IsSelected" Value="true">
<Setter Property="Background" Value="#FFECECEC"/>
<!--<Trigger Property="IsSelected" Value="False">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Foreground" Value="Black"/> <Setter Property="Foreground" Value="Black"/>
</Trigger> </Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="#ECECEC"/>
<Setter Property="Foreground" Value="Black"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="False">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Foreground" Value="Black"/>
</Trigger>-->
<!--<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property=""></Condition>
</MultiTrigger.Conditions>
</MultiTrigger>-->
</Style.Triggers> </Style.Triggers>
</Style> </Style>
@@ -81,7 +104,7 @@
BorderThickness="0" Foreground="{x:Null}" BorderThickness="0" Foreground="{x:Null}"
SelectedIndex="{Binding AppConfig.SelectedMenuIndex}" SelectedIndex="{Binding AppConfig.SelectedMenuIndex}"
VirtualizingPanel.VirtualizationMode="Recycling" VirtualizingPanel.VirtualizationMode="Recycling"
SelectionChanged="menus_SelectionChanged" SelectionChanged="Menu_SelectionChanged"
> >
<ListBox.Resources> <ListBox.Resources>
<ContextMenu x:Key="MenuDialog" Width="200"> <ContextMenu x:Key="MenuDialog" Width="200">
@@ -109,7 +132,7 @@
<ListBox.ItemTemplate> <ListBox.ItemTemplate>
<DataTemplate> <DataTemplate>
<StackPanel MouseLeftButtonDown="MenuClick" MouseRightButtonDown="MenuClick" Tag="{Binding}"> <StackPanel Tag="{Binding}">
<TextBox Text="{Binding Path=MenuName, Mode=TwoWay}" <TextBox Text="{Binding Path=MenuName, Mode=TwoWay}"
HorizontalAlignment="Left" HorizontalAlignment="Left"
Width="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type ListBox},AncestorLevel=1},Path=Tag, Mode=TwoWay, Converter={StaticResource MenuWidthConvert}, ConverterParameter=35}" Width="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type ListBox},AncestorLevel=1},Path=Tag, Mode=TwoWay, Converter={StaticResource MenuWidthConvert}, ConverterParameter=35}"
@@ -124,7 +147,9 @@
BorderThickness="0" BorderThickness="0"
IsVisibleChanged="MenuEditWhenVisibilityChanged" IsVisibleChanged="MenuEditWhenVisibilityChanged"
Visibility="{Binding MenuEdit}"/> Visibility="{Binding MenuEdit}"/>
<StackPanel Orientation="Horizontal"> <StackPanel Orientation="Horizontal"
IsVisibleChanged="MenuWhenVisibilityChanged"
Visibility="{Binding NotMenuEdit}">
<Button Background="Transparent" <Button Background="Transparent"
BorderThickness="0" BorderThickness="0"
hc:IconElement.Geometry="{Binding MenuGeometry}" hc:IconElement.Geometry="{Binding MenuGeometry}"
@@ -138,8 +163,6 @@
HorizontalAlignment="Center" HorizontalAlignment="Center"
TextAlignment="Center" TextAlignment="Center"
VerticalAlignment="Center" VerticalAlignment="Center"
IsVisibleChanged="MenuWhenVisibilityChanged"
Visibility="{Binding NotMenuEdit}"
/> />
</StackPanel> </StackPanel>
</StackPanel> </StackPanel>

View File

@@ -1,16 +1,18 @@
using DraggAnimatedPanelExample; using DraggAnimatedPanelExample;
using GeekDesk.Control.Windows; using GeekDesk.Control.Windows;
using GeekDesk.MyThread;
using GeekDesk.Util; using GeekDesk.Util;
using GeekDesk.ViewModel; using GeekDesk.ViewModel;
using System; using System;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Threading;
using System.Windows; using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
using System.Windows.Input; using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
namespace GeekDesk.Control.UserControls.PannelCard namespace GeekDesk.Control.UserControls.PannelCard
{ {
@@ -22,6 +24,9 @@ namespace GeekDesk.Control.UserControls.PannelCard
private int menuSelectIndexTemp = -1; private int menuSelectIndexTemp = -1;
private AppData appData = MainWindow.appData; private AppData appData = MainWindow.appData;
//是否正在修改菜单
private static bool IS_EDIT = false;
public LeftCardControl() public LeftCardControl()
{ {
InitializeComponent(); InitializeComponent();
@@ -60,13 +65,13 @@ namespace GeekDesk.Control.UserControls.PannelCard
} }
} }
////菜单点击事件 //////菜单点击事件
private void MenuClick(object sender, MouseButtonEventArgs e) //private void MenuClick(object sender, MouseButtonEventArgs e)
{ //{
//设置对应菜单的图标列表 // //设置对应菜单的图标列表
MenuInfo mi = (MenuInfo)(((StackPanel)sender).Tag); // MenuInfo mi = (MenuInfo)(((StackPanel)sender).Tag);
appData.AppConfig.SelectedMenuIcons = mi.IconList; // appData.AppConfig.SelectedMenuIcons = mi.IconList;
} //}
/// <summary> /// <summary>
/// 当修改菜单元素可见时 设置原菜单为不可见 并且不可选中 /// 当修改菜单元素可见时 设置原菜单为不可见 并且不可选中
@@ -76,9 +81,14 @@ namespace GeekDesk.Control.UserControls.PannelCard
/// <param name="e"></param> /// <param name="e"></param>
private void MenuWhenVisibilityChanged(object sender, DependencyPropertyChangedEventArgs e) private void MenuWhenVisibilityChanged(object sender, DependencyPropertyChangedEventArgs e)
{ {
TextBlock tb = sender as TextBlock; StackPanel sp = sender as StackPanel;
if (tb.Visibility == Visibility.Collapsed)
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) if (MenuListBox.SelectedIndex != -1)
{ {
menuSelectIndexTemp = MenuListBox.SelectedIndex; menuSelectIndexTemp = MenuListBox.SelectedIndex;
@@ -89,6 +99,31 @@ namespace GeekDesk.Control.UserControls.PannelCard
MenuListBox.SelectedIndex = menuSelectIndexTemp; MenuListBox.SelectedIndex = menuSelectIndexTemp;
} }
} }
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;
};
}
}
private void Lbi_MouseEnter(object sender, MouseEventArgs e)
{
ListBoxItem lbi = sender as ListBoxItem;
lbi.Background = Brushes.Transparent;
} }
/// <summary> /// <summary>
@@ -104,6 +139,27 @@ namespace GeekDesk.Control.UserControls.PannelCard
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;
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;
};
}
} }
/// <summary> /// <summary>
@@ -115,6 +171,7 @@ namespace GeekDesk.Control.UserControls.PannelCard
{ {
MenuInfo menuInfo = ((MenuItem)sender).Tag as MenuInfo; MenuInfo menuInfo = ((MenuItem)sender).Tag as MenuInfo;
menuInfo.MenuEdit = (int)Visibility.Visible; menuInfo.MenuEdit = (int)Visibility.Visible;
IS_EDIT = true;
} }
/// <summary> /// <summary>
@@ -134,7 +191,8 @@ namespace GeekDesk.Control.UserControls.PannelCard
if (index == 0) if (index == 0)
{ {
index = 0; index = 0;
} else }
else
{ {
index = index - 1; index = index - 1;
} }
@@ -153,6 +211,7 @@ namespace GeekDesk.Control.UserControls.PannelCard
/// <param name="e"></param> /// <param name="e"></param>
private void LostFocusOrEnterDown(object sender, EventArgs e) private void LostFocusOrEnterDown(object sender, EventArgs e)
{ {
bool done = true;
TextBox menuBox = null; TextBox menuBox = null;
if (e.GetType() == typeof(KeyEventArgs)) if (e.GetType() == typeof(KeyEventArgs))
{ {
@@ -161,12 +220,18 @@ namespace GeekDesk.Control.UserControls.PannelCard
{ {
menuBox = ((TextBox)sender); menuBox = ((TextBox)sender);
} }
else
{
done = false;
}
} }
else if (e.GetType() == typeof(RoutedEventArgs)) else if (e.GetType() == typeof(RoutedEventArgs))
{ {
menuBox = ((TextBox)sender); menuBox = ((TextBox)sender);
} }
if (done)
{
if (menuBox != null) if (menuBox != null)
{ {
MenuInfo menuInfo = menuBox.Tag as MenuInfo; MenuInfo menuInfo = menuBox.Tag as MenuInfo;
@@ -174,6 +239,9 @@ namespace GeekDesk.Control.UserControls.PannelCard
menuInfo.MenuName = text; menuInfo.MenuName = text;
menuInfo.MenuEdit = Visibility.Collapsed; menuInfo.MenuEdit = Visibility.Collapsed;
} }
IS_EDIT = false;
MenuListBox.SelectedIndex = menuSelectIndexTemp;
}
} }
/// <summary> /// <summary>
@@ -184,6 +252,7 @@ namespace GeekDesk.Control.UserControls.PannelCard
private void MenuEditWhenVisibilityChanged(object sender, DependencyPropertyChangedEventArgs e) private void MenuEditWhenVisibilityChanged(object sender, DependencyPropertyChangedEventArgs e)
{ {
TextBox box = sender as TextBox; TextBox box = sender as TextBox;
MenuInfo mi = box.Tag as MenuInfo;
if (box.Visibility == Visibility.Visible) if (box.Visibility == Visibility.Visible)
{ {
Keyboard.Focus(box); Keyboard.Focus(box);
@@ -202,17 +271,58 @@ namespace GeekDesk.Control.UserControls.PannelCard
IconfontWindow.Show(SvgToGeometry.GetIconfonts(), menuInfo); IconfontWindow.Show(SvgToGeometry.GetIconfonts(), menuInfo);
} }
private void menus_SelectionChanged(object sender, SelectionChangedEventArgs e) private void Menu_SelectionChanged(object sender, SelectionChangedEventArgs e)
{ {
//设置对应菜单的图标列表 //设置对应菜单的图标列表
if (MenuListBox.SelectedIndex == -1) if (MenuListBox.SelectedIndex == -1)
{ {
appData.AppConfig.SelectedMenuIcons = appData.MenuList[appData.MenuList.Count - 1].IconList; //appData.AppConfig.SelectedMenuIcons = appData.MenuList[appData.MenuList.Count - 1].IconList;
} }
else else
{ {
appData.AppConfig.SelectedMenuIcons = appData.MenuList[MenuListBox.SelectedIndex].IconList; appData.AppConfig.SelectedMenuIcons = appData.MenuList[MenuListBox.SelectedIndex].IconList;
} }
} }
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>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Menu_MouseEnter(object sender, MouseEventArgs e)
{
if (appData.AppConfig.HoverMenu && !IS_EDIT)
{
new Thread(() =>
{
Thread.Sleep(200);
this.Dispatcher.Invoke(() =>
{
ListBoxItem lbi = sender as ListBoxItem;
if (lbi.IsMouseOver)
{
int index = MenuListBox.ItemContainerGenerator.IndexFromContainer(lbi);
MenuListBox.SelectedIndex = index;
}
});
}).Start();
}
}
} }
} }