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>

 

转载于:https://www.cnblogs.com/3Tai/p/4156128.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值