原文地址: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的更多相关文章

  1. radiobutton group

    1. 环境:VS2010 2. 分组 将radio1.radio2.radio3分为1组,radio4.radio5分为另一组: 方法:设置  radio1  的 属性:  group.tabstop ...

  2. (转载)自定义CoordinatorLayout的Behavior(2):实现淘宝和QQ ToolBar透明渐变效果

    自定义CoordinatorLayout的Behavior(2):实现淘宝和QQ ToolBar透明渐变效果 作者 小武站台 关注 2016.02.19 11:34 字数 1244 阅读 3885评论 ...

  3. 转 Android RadioButton设置选中时文字和背景颜色同时改变

    主要应用在购物车,像淘宝的那样,点击以后弹出一个选择种类颜色这样的popuwindow以后,然后这个选择种类的地方要用到类似这个玩意儿. 搜了一下,效果和这个文章一致.转了. 原文地址:http:// ...

  4. 浅谈 underscore 内部方法 group 的设计原理

    前言 真是天一热什么事都不想干,这个月只产出了一篇文章,赶紧写一篇压压惊! 前文(https://github.com/hanzichi/underscore-analysis/issues/15)说 ...

  5. EUI RadioButton,RadioButtonGroup实现多选项按钮

    一 自动创建的RadioButtonGroup RadioButtonGroup不能在exml里拖动创建,也不能在exml源码里创建.因为wing没提供... 一个exml上摆放的多个RadioBut ...

  6. RaidoGroup+RadioButton模拟android下拉框弹出List

    引用 <上面的Hello world!是居左的,但是下面的文字却怎么都不能靠边.试了各种方法都不行.最后,无意中给RadioButton添加一个backgroud属性即可:<RadioBu ...

  7. Android 通过代码设置radiobutton不同方位图标的两种方法

    更换radiobutton中的图片在xml中很好设置,但对于初学者如何在代码中设置还是不容易找的.没法子,通过看原版api找到两个方法,setCompoundDrawables和setCompound ...

  8. 通过代码设置radiobutton不同方位图标的两种方法

    更换radiobutton中的图片在xml中很好设置,但对于初学者如何在代码中设置还是不容易找的.没法子,通过看原版api找到两个方法,setCompoundDrawables和setCompound ...

  9. tkinter第三章(单选和多选)RadioButton CheckButton

    最简单的CheckButton多选类 import tkinter as tk #checkButton的内容,多选 root = tk.Tk() v = tk.IntVar()#装整形变量的 #va ...

随机推荐

  1. asp.net mvc 4.0常见的几个问题

    看书的时候遇到很多不知所云的错误,都是在网上找到的解决方法,没办法,从asp.net到mcv很多的新技术,没有一点思路,只能在网上搜罗了. 1.更新产品不成功 更新产品的时候一直不能更新成功,但是很奇 ...

  2. Python迭代(入门8)

    转载请标明出处: http://www.cnblogs.com/why168888/p/6407980.html 本文出自:[Edwin博客园] Python迭代 1. 什么是迭代 注意: 集合是指包 ...

  3. copy "xxxxx\xx.dll xxxxxxx\ ” 已退出,代码为1 错误解决方法

    右键=>属性=>生成事件里面,查看预先生成事件命令行和后期生成事件命令行,查看复制的Dll是否存在已经路径是否正确

  4. JavaScript的DOM_节点的增删改

    一.概述 DOM 不单单可以查找节点,也可以创建节点.复制节点.插入节点.删除节点和替换节点.  二.write()方法 write()方法可以把任意字符串插入到文档中去.会覆盖掉原来的html &l ...

  5. [19/03/29-星期五] IO技术_File(文件)类(可操作文件,不能操作其里边内容,位于Java.io 包中)&递归遍历

    一.概念 java.io.File类:代表文件和目录. 在开发中,读取文件.生成文件.删除文件.修改文件的属性时经常会用到本类. 以pathname为路径创建File对象,如果pathname是相对路 ...

  6. 根据用户id生成一个唯一邀请码

    需求描述:根据用户id生成与之对应的唯一邀请码,范围为‘0-9A-Z’. 这个需求的重点在于加粗的部分,也就是要能够根据邀请码反推出用户ID,这样邀请码就不用入库了,在用户量很大的情况下,性能可以得到 ...

  7. 从零一起学Spring Boot之LayIM项目长成记(一) 初见 Spring Boot

    项目背景 之前写过LayIM的.NET版后端实现,后来又写过一版Java的.当时用的是servlet,websocket和jdbc.虽然时间过去很久了,但是仍有些同学在关注.偶然间我听说了Spring ...

  8. [转]超全面的.NET GDI+图形图像编程教程

    本篇主题内容是.NET GDI+图形图像编程系列的教程,不要被这个滚动条吓到,为了查找方便,我没有分开写,上面加了目录了,而且很多都是源码和图片~ GDI+绘图基础 编写图形程序时需要使用GDI(Gr ...

  9. 二十六、关于 IntelliJ IDEA 中 Schedule for Addition 的问题

    在我们使用 IntelliJ IDEA 的时候,经常会遇到这种情况,即: 从 SVN 检出项目之后,并用 IDEA 首次打开项目,IDEA 会弹出如下选择框: 如上图所示,让我们选择是否将XXX.im ...

  10. 【洛谷P2123】皇后游戏

    题目链接 这题的 实际上和"流水调度问题"是一样的 (我是不会告诉你我是看了讨论才知道的) 于是我就翻开了我们教练弄来的一本蓝不拉几的叫做"信息学奥赛一本通·提高篇&qu ...