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 ...
随机推荐
- 【转】[C++]实现委托模型
原文地址:http://www.cnblogs.com/zplutor/archive/2011/09/17/2179756.html 我对.Net的委托模型印象很深刻,使用委托,可以快速实现观察者模 ...
- UI层实现
领域驱动设计实践 —— UI层实现 目录 1. User Interface 2. Controller 3. DTO 4. infrastructure层的公共组件 5. UI层类图 6. ...
- redis持久化那些事(kēng)儿
这是一篇包含了介绍性质和吐槽性质的日志.主要介绍一下我学习redis持久化时候被坑的经历.redis的使用介绍现在没有打算写,因为比较多,以我如此懒的性格...好吧,还是有点这方面想法的,不过一篇博客 ...
- PHP数组和字符串相互转换以及判断字符串长度
这里只介绍最常用的方法: $array=explode(separator,$string); $string=implode(glue,$array); explode() 函数用来将字符串打散成数 ...
- CSS兼容性问题总结及解决方法
css兼容问题 兼容问题 1.文字本身的大小不兼容.同样是font-size:14px的宋体文字,在不同浏览器下占的空间是不一样的,ie下实际占高16px,下留白3px,ff下实际占高17px,上留白 ...
- 前端使用ajax传到后台的实体类的多个属性,直接用Map接收
前端ajax传过来的数据按照以上方法接收Map中 var ip = $("#ip").val(); var port = $("#port").val(); v ...
- 【题解】POJ 3417 Network(倍增求LCA+DP+树上差分)
POJ3417:http://poj.org/problem?id=3417 思路 我们注意到由“主要边”构成一颗树 “附加边”则是非树边 把一条附加边(x,y)加入树中 会与树上x,y之间构成一个环 ...
- MyEclipse 远程调试Tomcat
当Web项目部署在服务器之后,当项目出现问题的时候就需要远程调试[远程调试的代码要与本地代码一致] 配置远程调试的具体步骤如下: 1.Linux 中配置tomcat在catalina.sh中添加如下C ...
- tomcat文件夹下各文件夹的作用
1.bin目录主要是用来存放tomcat的命令,主要有两大类,一类是以.sh结尾的(linux命令),另一类是以.bat结尾的(windows命令). 很多环境变量的设置都在此处. 2.conf目录主 ...
- Oracle 左连接(+)加号用法及常用语法之间的关系
本文目的: 通过分析左连接(+)加号的写法和一些常用语法之间的联系,了解到Oracle 加号(+)的用法 分析步骤: 1.首先创建测试表的结构: create table test_left_a (a ...