您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章给大家介绍如何在WPF中自定义MenuItem样式,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
MenuItem的样式代码:
<!--MenuItem--> <Style TargetType="MenuItem"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="MenuItem"> <Grid ToolTip="{TemplateBinding Header}" Height="24" Background="#FBFBFD" Width="{TemplateBinding Width}"> <Grid.ColumnDefinitions> <ColumnDefinition Width="30" /> <ColumnDefinition Width="*" MinWidth="80"/> </Grid.ColumnDefinitions> <Border Background="#EBEBED"/> <Border x:Name="_border" Grid.ColumnSpan="2"/> <Image x:Name="_imgicon" Width="12" Height="12" Stretch="Uniform" Source="{Binding Icon,RelativeSource={RelativeSource TemplatedParent}}" /> <TextBlock Foreground="#2D2D30" Grid.Column="1" Margin="5 0 15 0" VerticalAlignment="Center" x:Name="_txt" Text="{Binding Header,RelativeSource={RelativeSource TemplatedParent}}" /> <Path Visibility="Collapsed" x:Name="_path" Grid.Column="1" HorizontalAlignment="Right" Margin="5 13 5 0" Data="M0,4 4,0 0,-4 z" Fill="#7E8A92"/> <Popup Placement="Right" x:Name="SubMenuPopup" AllowsTransparency="True" Focusable="False" IsOpen="{TemplateBinding IsSubmenuOpen}" > <ScrollViewer ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Disabled"> <Border Background="#ffc787" Name="SubmenuBorder" BorderBrush="LightGray" BorderThickness="1" SnapsToDevicePixels="True" > <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Cycle" /> </Border> </ScrollViewer> </Popup> </Grid> <ControlTemplate.Triggers> <Trigger Property="HasItems" Value="true"> <Setter TargetName="_path" Property="Visibility" Value="Visible"/> </Trigger> <Trigger Property="IsMouseOver" Value="true"> <Setter TargetName="_border" Property="Background" Value="#ffc787"/> <Setter TargetName="_txt" Property="Foreground" Value="White"/> <Setter TargetName="_path" Property="Fill" Value="White"/> </Trigger> <Trigger Property="IsEnabled" Value="false"> <Setter TargetName="_txt" Property="Foreground" Value="Gray"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style>
引用示例:
<Grid> <Grid.ContextMenu> <ContextMenu> <MenuItem Header="菜单一"> <MenuItem Header="子菜单"/> </MenuItem> <MenuItem Header="菜单二"></MenuItem> </ContextMenu> </Grid.ContextMenu> </Grid>
关于如何在WPF中自定义MenuItem样式就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。