WPF-单选MenuItem
这几天在做一个工具栏,用到了Menu控件,我们都知道它是条目控件,可以绑定数据源。自带的样式不太好看,于是自己就想修改一下,让它默认只有一个子项处于选中状态,再次打开会记录当前选中的是那个MenuItem.思路就是嵌入一个RadioButton控件并让其不可见,控制它的IsChecked属性与MenuItem的IsChecked属性之间的关系,支持添加Icon。
- Xaml及样式
<Window x:Class="WpfApp1Blend.Window1"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:WpfApp1Blend"mc:Ignorable="d"Title="Window1" Height="450" Width="800"><Window.Resources><SolidColorBrush x:Key="Window.ToolBar.Selected.FgColor" Color="#42a4ff"></SolidColorBrush><SolidColorBrush x:Key="Window.ToolBar.Selected.BgColor" Color="#0E2E58"></SolidColorBrush><SolidColorBrush x:Key="Window.ToolBar.MouseOver.BgColor" Color="#0a2243"></SolidColorBrush><SolidColorBrush x:Key="Window.ToolBar.BorderBrushColor" Color="#0d3256"></SolidColorBrush><SolidColorBrush x:Key="Window.ToolBar.BgColor" Color="#031527"></SolidColorBrush><SolidColorBrush x:Key="Control.Static.FgColor" Color="#A5C0E2"></SolidColorBrush><Style x:Key="TextBlock.Static13" TargetType="TextBlock"><Setter Property="Foreground" Value="{StaticResource Control.Static.FgColor}" /><Setter Property="FontFamily" Value="Microsoft YaHei"></Setter><Setter Property="TextWrapping" Value="Wrap" /><Setter Property="VerticalAlignment" Value="Center" /><Setter Property="HorizontalAlignment" Value="Center" /><Setter Property="Margin" Value="5"></Setter><Setter Property="FontSize" Value="13" /></Style><Style TargetType="{x:Type Menu}"><Setter Property="Background" Value="{StaticResource Window.ToolBar.BgColor}"></Setter></Style><Style TargetType="{x:Type MenuItem}"><Setter Property="IsCheckable" Value="True"></Setter><Setter Property="HorizontalAlignment" Value="Center"></Setter><Setter Property="VerticalAlignment" Value="Center"></Setter><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="{x:Type MenuItem}"><Border x:Name="Border_Ground" Background="Transparent"><StackPanel><Grid Margin="10,3"><Grid.ColumnDefinitions><ColumnDefinition Width="auto"></ColumnDefinition><ColumnDefinition Width="auto"></ColumnDefinition><ColumnDefinition></ColumnDefinition></Grid.ColumnDefinitions><RadioButton x:Name="rdo" IsChecked="{Binding IsChecked,RelativeSource={RelativeSource Mode=TemplatedParent},Mode=OneWay}" GroupName="A" IsThreeState="False" VerticalAlignment="Center" ></RadioButton><Image Grid.Column="1" Source="{Binding Icon,RelativeSource={RelativeSource Mode=TemplatedParent}}" Stretch="None" Margin="0,0,0,0"/><TextBlock x:Name="tb" Grid.Column="2" Text="{TemplateBinding Header}" Style="{StaticResource TextBlock.Static13}" HorizontalAlignment="Center" VerticalAlignment="Center"/></Grid><Popup x:Name="popup" AllowsTransparency="True" PopupAnimation="Fade" StaysOpen="False" VerticalOffset="5" IsOpen="{TemplateBinding IsChecked}" AutomationProperties.HelpText="121212"><Border Background="{StaticResource Window.ToolBar.BgColor}"><ItemsPresenter></ItemsPresenter></Border></Popup></StackPanel></Border><ControlTemplate.Triggers><Trigger Property="IsChecked" SourceName="rdo" Value="true"><Setter TargetName="Border_Ground" Property="Background" Value="{DynamicResource Window.ToolBar.Selected.BgColor}"/><Setter TargetName="tb" Property="Foreground" Value="{DynamicResource Window.ToolBar.Selected.FgColor}"/><Setter Property="IsChecked" Value="True"></Setter></Trigger><Trigger Property="IsPressed" Value="true"><Setter TargetName="tb" Property="Foreground" Value="{DynamicResource Window.ToolBar.Selected.FgColor}"/></Trigger><Trigger Property="IsMouseOver" Value="True"><Setter TargetName="Border_Ground" Property="Background" Value="{DynamicResource Window.ToolBar.MouseOver.BgColor}"/><Setter TargetName="Border_Ground" Property="BorderBrush" Value="{DynamicResource Window.ToolBar.BorderBrushColor}"/></Trigger></ControlTemplate.Triggers></ControlTemplate></Setter.Value></Setter></Style></Window.Resources><Grid Width="400" Height="400"><StackPanel><Menu><MenuItem Header="A"><MenuItem Header="A-1"/><MenuItem Header="A-2"/></MenuItem><MenuItem Header="B"><MenuItem Header="B-1"></MenuItem><MenuItem Header="B-2"></MenuItem><MenuItem Header="B-3"></MenuItem><MenuItem Header="B-4"></MenuItem></MenuItem></Menu></StackPanel></Grid>
</Window>
- 效果
不足之处就是弹出popup面板之后,需要点击外部两次才能隐藏popup,我想第一点击是radiobutton失去焦点,再次点击是为了隐藏popup控件。有兴趣的同学可以自己再优化一下。
Git:https://github.com/LXX1029/MenuItemSingleSelect.git
WPF-单选MenuItem相关推荐
- wpf mvvm MenuItem的Command事件
这是一个事件的辅助类,可以通过它实现MenuItem的Command事件 public class MyCommands : Freezable, ICommand, ICommandSource{p ...
- c# wpf 单选字体_WPF自定义控件与样式(1)-矢量字体图标(iconfont)
一.图标字体 图标字体在网页开发上运用非常广泛,具体可以网络搜索了解,网页上的运用有很多例子,如Bootstrap.但在C/S程序中使用还不多,字体图标其实就是把矢量图形打包到字体文件里,就像使用一般 ...
- c# wpf 单选字体_WPF中的字体改善
WPF4对字体渲染做了很大的改善,增加了TextOptions属性,该属性可以设置TextFormattingMode,TextRenderingMode,TextHintingMode 1.Text ...
- c# wpf 单选字体_WPF TextBlock字体设置
TextDecorations="Strikethrough" FontSize="72" FontFamily="Arial">A ...
- c# wpf 单选字体_WPF后台设置颜色字体等
Button TempButton = new Button(); TempButton.Tag = "按钮标记"; TempButton.Height = 30; TempBut ...
- wpf 代码获取contextmenu_[C#] 转:在WPF里面获取右键弹出菜单(ContextMenu)的鼠标点击源(Owner)控件...
WPF的ContextMenu没有类似WinForm里面ContextMenu.SourceControl的概念,但是你可以通过ContextMenuService的GetPlacementTarge ...
- WPF的ListBox中的RadioButton不能单选问题
WPF不知道是微软故意弄的还是真的匆忙的推出的产品,在实际开发过程中有很多小问题解决很麻烦. 今天主要说一下ListBox中使用RadioButton的时候里面的RadioButton不能单选!居然成 ...
- WPF 支持分组互斥的 RadioButton 式单选菜单
扩展 MenuItem 为同组互斥的 RadioMenuItem,并且将对勾符号修改为圆点. http://stackoverflow.com/a/35692688/5972372 这个问题下还有其他 ...
- wpf 可以取消的单选checkbox
利用radioButton的groupName分组互斥..再解决radiobutton的取消选择的问题.给radiobutton加了一个像checkbox的样式 2个方式: 效果图 第一种usecon ...
- wpf menuitem 点击控件元素_一招教你如何在WPF界面开发时自定义外观,快来Get
点击"了解更多"获取DevExpress v20.1完整版下载 通过DevExpress WPF Controls,您能创建有着强大互动功能的XAML基础应用程序,这些应用程序专注 ...
最新文章
- spring框架四大原则
- 1276: 求和游戏
- 合辑 | 面试必备!18篇Java面试疑难点详解
- jboss linux 性能,搭建jprofiler对jboss性能监控
- Mybatis in查询List或数组 场景实例
- python在数据分析的应用_Python运用于数据分析的简单教程
- 女生学的计算机专业有前途吗,计算机专业好不好 女生学计算机有前途吗
- SPSS 百度网盘免费下载
- [大话技术]话聊有趣的23种设计模式
- 论文学习——基于滑动窗口预测的水位时间序列异常检测
- python遇到错误跳过_python 如何跳过异常继续执行
- Android AlertDialog 对话框开启和关闭输入法问题
- 远程桌面退出后CPU过高的问题
- DNS(域名系统)介绍,深入解析DNS解析失败发生的原因及解决方法
- 阿里云服务器ECS计算型c6和c6e有什么区别?还有c6a和c6t性能对比
- java订餐系统_Java——吃货联盟订餐系统
- oem客户工程流程图_SFIS质量管理系统工作流程图
- vbs版的语音报时程序
- 什么是软件测试?软件测试的目的是什么?划分为哪几个阶段?
- Android剪切板功能
热门文章
- 电动自行车电池[充不进电][铅酸电池][饿死]重新激活
- Win系统设置热键提示 / 大写锁定提示 | 解决热键提示冲突
- 百看不如一练系列 32个python实战项目列表,得不到就毁掉
- 2021年拒绝躺平的三位Python号主!
- Adobe Acrobat 9 pro 打开pdf文档出现内容准备进度
- 宁海中考政策计算机考试合格,浙江宁海中考考试时间
- 【Inception-v3模型】迁移学习 实战训练 花朵种类识别
- 【Android适配问题集锦-音频类】当Android手机插入耳机后仍为外放的尴尬
- 黑马程序员 Java自学总结十六 网络编程
- hosts修改突然不起作用