Strange RadioButton group behavior with ToolBar
原文地址:https://social.msdn.microsoft.com/Forums/vstudio/zh-CN/83352293-ca52-4e22-8092-8e23c453bc75/strange-radiobutton-group-behavior-with-toolbar?forum=wpf
RadioButton's grouping implementation doesn't take into the consideration the scenario in which those RadioButtons belonging to the same group might reside in different visual tree. This is true when used inside ToolBar, because those content displayed in the over-flow section of ToolBar are actually residing in a Popup, the following code shows a possible workaround: Code Block
using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Controls;
using System.Windows.Controls.Primitives; namespace Sheva.Windows.Controls
{
public class WorkaroundToolBar : ToolBar
{
protected override void PrepareContainerForItemOverride(DependencyObject element, Object item)
{
base.PrepareContainerForItemOverride(element, item);
WorkaroundRadioButton radioButton = element as WorkaroundRadioButton;
if (radioButton != null)
{
radioButton.SetValue(DefaultStyleKeyProperty, ToolBar.RadioButtonStyleKey);
}
}
} public class WorkaroundRadioButton : RadioButton
{
protected override void OnChecked(RoutedEventArgs e)
{
Boolean bypassBuildinUncheckLogic = false;
DependencyObject parent = base.Parent;
if (parent != null)
{
foreach (DependencyObject logicalChild in LogicalTreeHelper.GetChildren(parent))
{
// If there is any logical child connected to the popup, we should employ our own invention to uncheck the group.
if (GetPopupFromVisualChild(logicalChild as Visual) != null)
{
bypassBuildinUncheckLogic = true;
}
} if (bypassBuildinUncheckLogic)
{
foreach (DependencyObject logicalChild in LogicalTreeHelper.GetChildren(parent))
{
WorkaroundRadioButton radioButton = logicalChild as WorkaroundRadioButton;
if (radioButton != this && !String.IsNullOrEmpty(radioButton.GroupName) && radioButton.GroupName == base.GroupName)
{
radioButton.IsChecked = false;
}
}
}
} if (!bypassBuildinUncheckLogic)
{
base.OnChecked(e);
}
} private static Popup GetPopupFromVisualChild(Visual child)
{
Visual parent = child;
FrameworkElement visualRoot = null;
while (parent != null)
{
visualRoot = parent as FrameworkElement;
parent = VisualTreeHelper.GetParent(parent) as Visual;
} Popup popup = null;
if (visualRoot != null)
{
popup = visualRoot.Parent as Popup;
} return popup;
}
}
}
<Window x:Class="AnswerHarness.ToolBarGroupNameProblem"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cc="clr-namespace:Sheva.Windows.Controls"
Title="ToolBarGroupNameProblem" Height="" Width="">
<DockPanel>
<cc:WorkaroundToolBar Height="" VerticalAlignment="Top" DockPanel.Dock="Top">
<cc:WorkaroundRadioButton GroupName="Group1" Content="Radio1" x:Name="Radio1"/>
<cc:WorkaroundRadioButton GroupName="Group1" Content="Radio2" x:Name="Radio2"/>
<cc:WorkaroundRadioButton GroupName="Group1" Content="Radio3" x:Name="Radio3"/>
<cc:WorkaroundRadioButton GroupName="Group1" Content="Radio4" x:Name="Radio4"/>
</cc:WorkaroundToolBar>
<Button Width="" Height="" Name="btn"/>
</DockPanel>
</Window>
Strange RadioButton group behavior with ToolBar的更多相关文章
- radiobutton group
1. 环境:VS2010 2. 分组 将radio1.radio2.radio3分为1组,radio4.radio5分为另一组: 方法:设置 radio1 的 属性: group.tabstop ...
- (转载)自定义CoordinatorLayout的Behavior(2):实现淘宝和QQ ToolBar透明渐变效果
自定义CoordinatorLayout的Behavior(2):实现淘宝和QQ ToolBar透明渐变效果 作者 小武站台 关注 2016.02.19 11:34 字数 1244 阅读 3885评论 ...
- 转 Android RadioButton设置选中时文字和背景颜色同时改变
主要应用在购物车,像淘宝的那样,点击以后弹出一个选择种类颜色这样的popuwindow以后,然后这个选择种类的地方要用到类似这个玩意儿. 搜了一下,效果和这个文章一致.转了. 原文地址:http:// ...
- 浅谈 underscore 内部方法 group 的设计原理
前言 真是天一热什么事都不想干,这个月只产出了一篇文章,赶紧写一篇压压惊! 前文(https://github.com/hanzichi/underscore-analysis/issues/15)说 ...
- EUI RadioButton,RadioButtonGroup实现多选项按钮
一 自动创建的RadioButtonGroup RadioButtonGroup不能在exml里拖动创建,也不能在exml源码里创建.因为wing没提供... 一个exml上摆放的多个RadioBut ...
- RaidoGroup+RadioButton模拟android下拉框弹出List
引用 <上面的Hello world!是居左的,但是下面的文字却怎么都不能靠边.试了各种方法都不行.最后,无意中给RadioButton添加一个backgroud属性即可:<RadioBu ...
- Android 通过代码设置radiobutton不同方位图标的两种方法
更换radiobutton中的图片在xml中很好设置,但对于初学者如何在代码中设置还是不容易找的.没法子,通过看原版api找到两个方法,setCompoundDrawables和setCompound ...
- 通过代码设置radiobutton不同方位图标的两种方法
更换radiobutton中的图片在xml中很好设置,但对于初学者如何在代码中设置还是不容易找的.没法子,通过看原版api找到两个方法,setCompoundDrawables和setCompound ...
- tkinter第三章(单选和多选)RadioButton CheckButton
最简单的CheckButton多选类 import tkinter as tk #checkButton的内容,多选 root = tk.Tk() v = tk.IntVar()#装整形变量的 #va ...
随机推荐
- 使用Jmeter进行接口测试和压力测试的配置和使用
1. Jmeter简介 Apache JMeter是Apache组织开发的基于Java的压力测试工具.用于对软件做压力测试,它最初被设计用于Web应用测试,但后来扩展到其他测试领域. JMeter 可 ...
- ROC曲线手画
绘图过程很简单:给定m个正例子,n个反例子,根据学习器预测结果进行排序,先把分类阈值设为最大,使得所有例子均预测为反例,此时TPR和FPR均为0,在(0,0)处标记一个点,再将分类阈值依次设为每个样例 ...
- Allocate exception for servlet ValidateUsernameServlet 异常
如果eclipse无法对类文件进行编译那么运行时就会发生 Allocate exception for servlet ValidateUsernameServlet 异常,说找不到类.
- Springmvc 进行数据类型转换
SpringMVC进行一些常用的数据类型转换,这里以Date 数据类型的转换为例. SpringMVC表单中输入日期,一般都是以字符串的形式输入,如何将字符形式的日期转换为Date 类型的呢?这里只需 ...
- 阅读layim代码小记,监听事件实现方法
(function (win) { //注册事件 var chat = function () { $('#open').on('click', function () { sendMessage() ...
- Selenium应用代码(登录)
这篇可以不看,主要是为了以后的应用代码(传参)做铺垫. import java.awt.Rectangle; import java.awt.image.BufferedImage;import ja ...
- Lambda使用
说明 本文内容来自 [Java8 In Action] 一书 四种方法引用类型 类型 示例 引用静态方法 ContainingClass::staticMethodName 引用某个对象的实例方法 c ...
- Tag It 一款 Jquery控件,当你在文本框中输入逗号时,自动帮你分隔开相关内容
Demo地址:http://webspirited.com/tagit/ 使用方法: 除了JQuery脚本外,下面的脚本也是必须的,这些脚本你都可以去GitHub下载:https://github.c ...
- 前端基础-CSS的属性相关设置
一 字体属性 二 文本属性 三 背景属性 四 盒子模型 五 盒子模型各部分详解 一. 字体属性 1.font-weight:文字粗细(表格中*为重点) 取值 描述 normal 默认值,标准粗细 ...
- 一点一点看JDK源码(五)java.util.ArrayList 后篇之forEach
一点一点看JDK源码(五)java.util.ArrayList 后篇之forEach liuyuhang原创,未经允许禁止转载 本文举例使用的是JDK8的API 目录:一点一点看JDK源码(〇) 代 ...