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>
  • 相关阅读:
    测开之路一百一十:bootstrap图片
    测开之路一百零九:bootstrap列表
    测开之路一百零八:bootstrap表格
    测开之路一百零七:bootstrap排版
    测开之路一百零六:bootstrap布局
    学生管理之原生分页方法
    Ajax文件上传三式
    学生管理之模板继承
    Django之Models的class Meta
    [C++]指针/指针数组/数组指针/多维指针/单值指针/多值指针
  • 原文地址:https://www.cnblogs.com/3Tai/p/4156128.html
Copyright © 2011-2022 走看看