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>
  • 相关阅读:
    Android 屏幕适配问题分析
    Android应用程序结构
    国内市场各品牌手机后门介绍
    获取Android设备WIFI的MAC地址 “MAC地址”
    Android 如何判断指定服务是否在运行中 “Service”
    Android强制关闭某个指定应用 “关闭应用”
    如何在Android中的Activity启动第三方应用程序?
    js事件(十二)
    js文本对象模型[DOM]【续】(Node节点类型)
    js文本对象模型【DOM】(十一)
  • 原文地址:https://www.cnblogs.com/3Tai/p/4156128.html
Copyright © 2011-2022 走看看