zoukankan      html  css  js  c++  java
  • 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="300" Width="300"> <DockPanel> <cc:WorkaroundToolBar Height="30" 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="120" Height="30" Name="btn"/> </DockPanel> </Window>
  • 相关阅读:
    rpm 和 yum 的使用技巧
    启动Hadoop时遇到Name or service not knownstname 错误
    使用Pod集成Bugtags填坑记
    xcode 上 crash 调试的三种方法
    在MAC上安装虚拟机搭建Ubuntu开发环境
    shell复习---文件解压命令
    XCODE7新变化之-test
    Object-C单元测试&MOCK(摘录精选)
    shell复习笔记----查找与替换
    shell复习笔记----命令与参数
  • 原文地址:https://www.cnblogs.com/3Tai/p/4156128.html
Copyright © 2011-2022 走看看