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 ...
随机推荐
- June 26th 2017 Week 26th Monday
I am a little tempted, but so afraid to look in your eyes. 我对你有一点动心,却如此害怕看你的眼睛. Now that you are tem ...
- bzoj1818 [Cqoi2010]内部白点
Description 无限大正方形网格里有n个黑色的顶点,所有其他顶点都是白色的(网格的顶点即坐标为整数的点,又称整点).每秒钟,所有内部白点同时变黑,直到不存在内部白点为止.你的任务是统计最后网格 ...
- CF311E Biologist
嘟嘟嘟 很显然是一道最小割模型. 做完几道题后.图的大概就能想出来了: 1.对于每一个动物,如果是0,就和s连一条边,否则向t连一条边. 2.对于每一个任务,题中要求最大利润,可以转化成最小损失. ( ...
- 快速理解Event事件
浏览器事件是所有web程序的核心.javascript与HTML之间的交互是通过事件实现的.通过这些事件我们定义将要发生的行为.事件是一种异步编程的实现方式,本质上是程序各个组成部分之间的通信. 1. ...
- idea debug操作
3. 条件断点 说明: 调试的时候,在循环里增加条件判断,可以极大的提高效率,心情也能愉悦.具体操作: 在断点处右击调出条件断点.可以在满足某个条件下,实施断点. 查看表达式的值(Ctrl + u): ...
- python3爬虫编码问题
使用爬虫爬取网页经常遇到各种编码问题,因此产生乱码今天折腾了一天,全部总结一遍环境:win10,pycharm,python3.41.首先先来网页编码是utf-8的:以百度首页为例:使用request ...
- Java Bean与Map之间相互转化的实现
目录树 概述 Apache BeanUtils将Bean转Map Apache BeanUtils将Map转Bean 理解BeanUtils将Bean转Map的实现之手写Bean转Map 概述 Apa ...
- Adnroid studio 无法 Build APK(s)
报错 Error:java.nio.file.AccessDeniedException: C:\Program Files\Android\android-sdk-windows\.android\ ...
- JAVA基础之控制台输入输出
---恢复内容开始--- 输入需要用scanner机制 代码: 启用scanner机制 Scanner input = new Scanner(System.in); //String x= inpu ...
- 【转载】JavaScript导出Excel
[转载]JavaScript导出Excel 原文地址 如果没有用到前端插件,也没有用到后台poi导出的话,用js导出也是一种方式.亲测可用. /** * 导出excel */ var idTmr; f ...