原文地址: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. node-7.2.1 already installed, it's just not linked

    直接在terminal下运行以卸载node和nvm: sudo rm -rf /usr/local/{bin/{node,npm},lib/node_modules/npm,lib/node,shar ...

  2. 基于SAP Kyma的订单编排增强介绍

    尽管有一万个舍不得,2018年还是无可挽回地离我们远去了. 唯有SAP成都研究院的同事和我去年在网络上留下的这些痕迹,能证明2018年我们曾经很认真地去度过每一天: SAP成都研究院2018年总共87 ...

  3. Linux学习总结(一) windos环境vmware安装centos7

    一.在这里我先简单介绍下虚拟化技术[即在一台电脑上虚拟出子系统,而且可能需要多台服务器进行工作,一般都是linux系统做服务器或者学习研究之用], 二来我们可以有效利用一台pc的资源模拟出终端远程访问 ...

  4. Junit报错Initialization Error

    出错原因是没有把方法声明为public

  5. Tomcat整体介绍

    来源 本文整理自 <Tomcat内核设计剖析>.<Tomcat结构解析> Tomcat 整体架构 ​ 如上图所示:包含了Tomcat内部的主要组件,每个组件之间的层次包含关系很 ...

  6. centos7生成密钥及集群之间免密登录

    1.在本地生成密钥 命令:ssh-keygen -t dsa -P '' -f ~/.ssh/id_dsa 2.进入.ssh文件夹,创建保存密钥的文件 命令:touch authorized_keys ...

  7. python中的sum函数.sum(axis=1)

    看起来挺简单的样子,但是在给sum函数中加入参数.sum(a,axis=0)或者是.sum(axis=1) 就有点不解了 在我实验以后发现 我们平时用的sum应该是默认的axis=0 就是普通的相加 ...

  8. 【题解】洛谷P2375 [NOI2014] 动物园(KMP)

    洛谷P2375:https://www.luogu.org/problemnew/show/P2375 思路 这道题可以说是完全刷新了本蒟蒻对KMP的理解 感觉对next数组的理解上升到一个新的高度 ...

  9. HDU 2097 sky数 (进制转化)

    传送门: Sky数 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  10. Python 学习笔记(九)Python元组和字典(一)

    Python 元组 元组的定义  元组(tuple)是一种Python对象类型,元组也是一种序列 Python中的元组与列表类似,不同之处元组的元素不能修改 元组使用小括号,列表使用方括号 元组的创建 ...