优化修改菜单图标时的样式及逻辑

This commit is contained in:
liufei
2022-01-26 10:20:36 +08:00
parent 0495b8a121
commit 5a89b40244
5 changed files with 164 additions and 63 deletions

View File

@@ -22,6 +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"/>
<Style.Triggers> <Style.Triggers>
<MultiTrigger> <MultiTrigger>
<MultiTrigger.Conditions> <MultiTrigger.Conditions>
@@ -42,24 +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="IsMouseOver" Value="True">
<Setter Property="Background" Value="#FFECECEC"/>
</Trigger> <!--<Trigger Property="IsSelected" Value="False">
<Trigger Property="IsSelected" Value="True"> <Setter Property="Background" Value="Transparent"/>
<Setter Property="Background" Value="#FFECECEC"/>
<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>-->
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding IsEdit}" Value="True"/>
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter Property="Background" Value="Transparent"/>
</MultiDataTrigger.Setters>
</MultiDataTrigger>
</Style.Triggers> </Style.Triggers>
</Style> </Style>
@@ -87,14 +98,14 @@
</hc:Card.ContextMenu> </hc:Card.ContextMenu>
<WrapPanel Orientation="Horizontal"> <WrapPanel Orientation="Horizontal">
<ListBox x:Name="MenuListBox" <ListBox x:Name="MenuListBox"
Padding="2,3,0,2" Padding="2,3,0,2"
ItemsSource="{Binding MenuList}" ItemsSource="{Binding MenuList}"
Tag="{Binding AppConfig.MenuCardWidth}" Tag="{Binding AppConfig.MenuCardWidth}"
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="Menu_SelectionChanged" SelectionChanged="Menu_SelectionChanged"
> >
<ListBox.Resources> <ListBox.Resources>
<ContextMenu x:Key="MenuDialog" Width="200"> <ContextMenu x:Key="MenuDialog" Width="200">
<MenuItem Header="新建菜单" Click="CreateMenu"/> <MenuItem Header="新建菜单" Click="CreateMenu"/>
@@ -123,36 +134,36 @@
<DataTemplate> <DataTemplate>
<StackPanel 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}"
FontSize="16" FontSize="16"
Height="28" Height="28"
VerticalAlignment="Center" VerticalAlignment="Center"
TextAlignment="Left" TextAlignment="Left"
LostFocus="LostFocusOrEnterDown" LostFocus="LostFocusOrEnterDown"
KeyDown="LostFocusOrEnterDown" KeyDown="LostFocusOrEnterDown"
Tag="{Binding}" Tag="{Binding}"
Padding="2" Padding="2"
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}"
hc:IconElement.Height="18" hc:IconElement.Height="18"
hc:IconElement.Width="18" hc:IconElement.Width="18"
IsEnabled="False" IsEnabled="False"
Opacity="1" Opacity="1"
Foreground="{Binding GeometryColor}" Foreground="{Binding GeometryColor}"
/> />
<TextBlock Text="{Binding MenuName}" <TextBlock Text="{Binding MenuName}"
HorizontalAlignment="Center" HorizontalAlignment="Center"
TextAlignment="Center" TextAlignment="Center"
VerticalAlignment="Center" VerticalAlignment="Center"
IsVisibleChanged="MenuWhenVisibilityChanged" />
Visibility="{Binding NotMenuEdit}"
/>
</StackPanel> </StackPanel>
</StackPanel> </StackPanel>
</DataTemplate> </DataTemplate>

View File

@@ -11,6 +11,7 @@ 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; using System.Windows.Threading;
namespace GeekDesk.Control.UserControls.PannelCard namespace GeekDesk.Control.UserControls.PannelCard
@@ -23,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();
@@ -77,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,9 +98,33 @@ 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>
/// 新建菜单 /// 新建菜单
/// </summary> /// </summary>
@@ -105,6 +138,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>
@@ -116,6 +170,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>
@@ -154,6 +209,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,19 +217,26 @@ namespace GeekDesk.Control.UserControls.PannelCard
if (eKey.Key == Key.Enter) if (eKey.Key == Key.Enter)
{ {
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 (menuBox != null) if (done)
{ {
MenuInfo menuInfo = menuBox.Tag as MenuInfo; if (menuBox != null)
string text = menuBox.Text; {
menuInfo.MenuName = text; MenuInfo menuInfo = menuBox.Tag as MenuInfo;
menuInfo.MenuEdit = Visibility.Collapsed; string text = menuBox.Text;
menuInfo.MenuName = text;
menuInfo.MenuEdit = Visibility.Collapsed;
}
IS_EDIT = false;
MenuListBox.SelectedIndex = menuSelectIndexTemp;
} }
} }
@@ -185,6 +248,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);
@@ -208,7 +272,7 @@ namespace GeekDesk.Control.UserControls.PannelCard
//设置对应菜单的图标列表 //设置对应菜单的图标列表
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
{ {
@@ -216,6 +280,20 @@ 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>
/// 鼠标悬停切换菜单 /// 鼠标悬停切换菜单
/// </summary> /// </summary>
@@ -223,7 +301,7 @@ namespace GeekDesk.Control.UserControls.PannelCard
/// <param name="e"></param> /// <param name="e"></param>
private void Menu_MouseEnter(object sender, MouseEventArgs e) private void Menu_MouseEnter(object sender, MouseEventArgs e)
{ {
if (appData.AppConfig.HoverMenu) if (appData.AppConfig.HoverMenu && !IS_EDIT)
{ {
new Thread(() => new Thread(() =>
{ {
@@ -241,5 +319,6 @@ namespace GeekDesk.Control.UserControls.PannelCard
} }
} }
} }
} }

View File

@@ -22,6 +22,7 @@
SizeChanged="Window_SizeChanged" SizeChanged="Window_SizeChanged"
KeyDown="OnKeyDown" KeyDown="OnKeyDown"
Focusable="True" Focusable="True"
MouseDown="MainWindow_MouseDown"
> >
<WindowChrome.WindowChrome> <WindowChrome.WindowChrome>
<WindowChrome CaptionHeight="0" ResizeBorderThickness="15"/> <WindowChrome CaptionHeight="0" ResizeBorderThickness="15"/>
@@ -73,6 +74,7 @@
<ColumnDefinition Width="*"></ColumnDefinition> <ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions> </Grid.ColumnDefinitions>
<TextBox x:Name="EmptyTextBox" Width="0"/>
<DockPanel Grid.Row="0" Grid.Column="0" MouseMove="DragMove"> <DockPanel Grid.Row="0" Grid.Column="0" MouseMove="DragMove">
<DockPanel.Background> <DockPanel.Background>

View File

@@ -524,7 +524,14 @@ namespace GeekDesk
} }
} }
/// <summary>
/// 为了让修改菜单的textBox失去焦点
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MainWindow_MouseDown(object sender, MouseButtonEventArgs e)
{
EmptyTextBox.Focus();
}
} }
} }

View File

@@ -24,8 +24,10 @@ namespace GeekDesk.ViewModel
private string geometryColor; //几何图标颜色 private string geometryColor; //几何图标颜色
private ObservableCollection<IconInfo> iconList = new ObservableCollection<IconInfo>(); private ObservableCollection<IconInfo> iconList = new ObservableCollection<IconInfo>();
[field: NonSerializedAttribute()] [field: NonSerializedAttribute()]
private static string[] NO_WRITE_ARR = new string[] { "IsEdit" }; private static string[] NO_WRITE_ARR = new string[] { "IsEdit"};
public bool IsEdit public bool IsEdit