gpt4 book ai didi

设置了 Image 和 IsCheckable 的 WPF MenuItem

转载 作者:行者123 更新时间:2023-12-02 22:47:46 24 4
gpt4 key购买 nike

我注意到,如果您设置 IsCheckable 并为 MenuItem 设置图像,则在选中该项目时,该图像会消失。是否可以使其与旧的 .Net 2.0 类似地工作,以便在检查时图像周围有边框?谢谢保罗。

最佳答案

您需要重新设计菜单项才能实现这一点。您可以从 here 获取默认样式。 ,例如 Aero,然后拉出 MenuItem 的样式(和相关画笔)。一旦有了它,您就可以根据需要进行自定义。

对于 MenuItem,您实际上可以重新定义 SubmenuItemTemplateKey 和 SubmenuHeaderTemplateKey ControlTemplate,方法是将其添加到您的应用程序资源中(从上面的文件中提取并进行调整):

<Application x:Class="MyApplication.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:theme="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
StartupUri="MainWindow.xaml">
<Application.Resources>

<Geometry x:Key="RightArrow">M 0,0 L 4,3.5 L 0,7 Z</Geometry>
<Geometry x:Key="Checkmark">M 0,5.1 L 1.7,5.2 L 3.4,7.1 L 8,0.4 L 9.2,0 L 3.3,10.8 Z</Geometry>

<LinearGradientBrush x:Key="MenuItemSelectionFill"
StartPoint="0,0"
EndPoint="0,1">
<LinearGradientBrush.GradientStops>
<GradientStop Color="#34C5EBFF"
Offset="0" />
<GradientStop Color="#3481D8FF"
Offset="1" />
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>

<ControlTemplate x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type MenuItem}, ResourceId=SubmenuItemTemplateKey}"
TargetType="{x:Type MenuItem}">
<Grid SnapsToDevicePixels="true">
<Rectangle Name="Bg"
Fill="{TemplateBinding MenuItem.Background}"
Stroke="{TemplateBinding MenuItem.BorderBrush}"
StrokeThickness="1"
RadiusX="2"
RadiusY="2" />
<Rectangle x:Name="InnerBorder"
Margin="1"
RadiusX="2"
RadiusY="2" />
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition MinWidth="24"
Width="Auto"
SharedSizeGroup="MenuItemIconColumnGroup" />
<ColumnDefinition Width="4" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="37" />
<ColumnDefinition Width="Auto"
SharedSizeGroup="MenuItemIGTColumnGroup" />
<ColumnDefinition Width="17" />
</Grid.ColumnDefinitions>
<Border x:Name="GlyphPanel"
Background="Transparent"
BorderBrush="Transparent"
BorderThickness="1"
CornerRadius="3"
Margin="1"
Width="22"
Height="22">
<Grid>
<Path Name="Glyph"
Width="9"
Height="11"
Fill="#0C12A1"
FlowDirection="LeftToRight"
Data="{StaticResource Checkmark}"
Visibility="Collapsed" />
<ContentPresenter x:Name="Icon"
Margin="1"
VerticalAlignment="Center"
ContentSource="Icon"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
</Grid>
</Border>
<ContentPresenter Grid.Column="2"
ContentSource="Header"
Margin="{TemplateBinding MenuItem.Padding}"
RecognizesAccessKey="True"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
<TextBlock Grid.Column="4"
Text="{TemplateBinding MenuItem.InputGestureText}"
Margin="{TemplateBinding MenuItem.Padding}" />
</Grid>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="Icon"
Value="{x:Null}">
<Setter TargetName="Icon"
Property="Visibility"
Value="Collapsed" />
</Trigger>
<Trigger Property="IsChecked"
Value="true">
<Setter TargetName="GlyphPanel"
Property="Background"
Value="#E6EFF4" />
<Setter TargetName="GlyphPanel"
Property="BorderBrush"
Value="#CDD3E6" />
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="Icon"
Value="{x:Null}" />
<Condition Property="IsChecked"
Value="true" />
</MultiTrigger.Conditions>
<Setter TargetName="Glyph"
Property="Visibility"
Value="Visible" />
<Setter TargetName="Icon"
Property="Visibility"
Value="Collapsed" />
</MultiTrigger>
<Trigger Property="IsHighlighted"
Value="true">
<Setter TargetName="Bg"
Property="Fill"
Value="{StaticResource MenuItemSelectionFill}" />
<Setter TargetName="Bg"
Property="Stroke"
Value="#8071CBF1" />
<Setter TargetName="InnerBorder"
Property="Stroke"
Value="#40FFFFFF" />
</Trigger>
<Trigger Property="IsEnabled"
Value="false">
<Setter Property="Foreground"
Value="#FF9A9A9A" />
<Setter TargetName="GlyphPanel"
Property="Background"
Value="#EEE9E9" />
<Setter TargetName="GlyphPanel"
Property="BorderBrush"
Value="#DBD6D6" />
<Setter TargetName="Glyph"
Property="Fill"
Value="#848589" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
<ControlTemplate x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type MenuItem}, ResourceId=SubmenuHeaderTemplateKey}"
TargetType="{x:Type MenuItem}">
<Grid SnapsToDevicePixels="true">
<Rectangle Name="Bg"
Fill="{TemplateBinding MenuItem.Background}"
Stroke="{TemplateBinding MenuItem.BorderBrush}"
StrokeThickness="1"
RadiusX="2"
RadiusY="2" />
<Rectangle x:Name="InnerBorder"
Margin="1"
Stroke="Transparent"
StrokeThickness="1"
RadiusX="2"
RadiusY="2" />
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition MinWidth="24"
Width="Auto"
SharedSizeGroup="MenuItemIconColumnGroup" />
<ColumnDefinition Width="4" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="37" />
<ColumnDefinition Width="Auto"
SharedSizeGroup="MenuItemIGTColumnGroup" />
<ColumnDefinition Width="17" />
</Grid.ColumnDefinitions>
<Border x:Name="GlyphPanel"
Background="Transparent"
BorderBrush="Transparent"
BorderThickness="1"
CornerRadius="3"
Margin="1"
Width="22"
Height="22">
<Grid>
<Path Name="Glyph"
Width="9"
Height="11"
Fill="#0C12A1"
FlowDirection="LeftToRight"
Data="{StaticResource Checkmark}"
Visibility="Collapsed" />
<ContentPresenter x:Name="Icon"
Margin="1"
VerticalAlignment="Center"
ContentSource="Icon"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
</Grid>
</Border>
<ContentPresenter Grid.Column="2"
ContentSource="Header"
Margin="{TemplateBinding MenuItem.Padding}"
RecognizesAccessKey="True"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
<TextBlock Grid.Column="4"
Text="{TemplateBinding MenuItem.InputGestureText}"
Margin="{TemplateBinding MenuItem.Padding}"
Visibility="Collapsed" />
<Path Grid.Column="5"
VerticalAlignment="Center"
Margin="4,0,0,0"
Fill="{TemplateBinding MenuItem.Foreground}"
Data="{StaticResource RightArrow}" />
</Grid>
<Popup x:Name="PART_Popup"
AllowsTransparency="true"
Placement="Right"
VerticalOffset="-3"
HorizontalOffset="-2"
IsOpen="{Binding Path=IsSubmenuOpen,RelativeSource={RelativeSource TemplatedParent}}"
Focusable="false"
PopupAnimation="{DynamicResource {x:Static SystemParameters.MenuPopupAnimationKey}}">
<theme:SystemDropShadowChrome Name="Shdw"
Color="Transparent">
<ContentControl Name="SubMenuBorder"
Template="{DynamicResource {ComponentResourceKey TypeInTargetAssembly={x:Type FrameworkElement}, ResourceId=SubmenuContent}}"
IsTabStop="false">
<ScrollViewer CanContentScroll="true"
Style="{DynamicResource {ComponentResourceKey TypeInTargetAssembly={x:Type FrameworkElement}, ResourceId=MenuScrollViewer}}">
<ItemsPresenter Margin="2"
KeyboardNavigation.TabNavigation="Cycle"
KeyboardNavigation.DirectionalNavigation="Cycle"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
Grid.IsSharedSizeScope="true" />
</ScrollViewer>
</ContentControl>
</theme:SystemDropShadowChrome>
</Popup>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsSuspendingPopupAnimation"
Value="true">
<Setter TargetName="PART_Popup"
Property="PopupAnimation"
Value="None" />
</Trigger>
<Trigger Property="IsHighlighted"
Value="true">
<Setter TargetName="InnerBorder"
Property="Stroke"
Value="#D1DBF4FF" />
</Trigger>
<Trigger Property="Icon"
Value="{x:Null}">
<Setter TargetName="Icon"
Property="Visibility"
Value="Collapsed" />
</Trigger>
<Trigger Property="IsChecked"
Value="true">
<Setter TargetName="GlyphPanel"
Property="Background"
Value="#E6EFF4" />
<Setter TargetName="GlyphPanel"
Property="BorderBrush"
Value="#CDD3E6" />
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="Icon"
Value="{x:Null}" />
<Condition Property="IsChecked"
Value="true" />
</MultiTrigger.Conditions>
<Setter TargetName="Glyph"
Property="Visibility"
Value="Visible" />
<Setter TargetName="Icon"
Property="Visibility"
Value="Collapsed" />
</MultiTrigger>
<Trigger SourceName="PART_Popup"
Property="Popup.HasDropShadow"
Value="true">
<Setter TargetName="Shdw"
Property="Margin"
Value="0,0,5,5" />
<Setter TargetName="Shdw"
Property="Color"
Value="#71000000" />
</Trigger>
<Trigger Property="IsHighlighted"
Value="true">
<Setter TargetName="Bg"
Property="Fill"
Value="{StaticResource MenuItemSelectionFill}" />
<Setter TargetName="Bg"
Property="Stroke"
Value="#8571CBF1" />
</Trigger>
<Trigger Property="IsEnabled"
Value="false">
<Setter Property="Foreground"
Value="#FF9A9A9A" />
<Setter TargetName="GlyphPanel"
Property="Background"
Value="#EEE9E9" />
<Setter TargetName="GlyphPanel"
Property="BorderBrush"
Value="#DBD6D6" />
<Setter TargetName="Glyph"
Property="Fill"
Value="#848589" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>

</Application.Resources>
</Application>

您需要将对PresentationSource.Aero 的引用添加到您的项目中,并且您可能希望在选中时使GlyphPanel 的颜色变暗。

关于设置了 Image 和 IsCheckable 的 WPF MenuItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3842493/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com