码迷,mamicode.com
首页 > 其他好文 > 详细

Strange RadioButton group behavior with ToolBar

时间:2014-12-10 19:42:10      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   os   sp   for   

原文地址:https://social.msdn.microsoft.com/Forums/vstudio/zh-CN/83352293-ca52-4e22-8092-8e23c453bc75/strange-radiobutton-group-behavior-with-toolbar?forum=wpf

 


RadioButtons grouping implementation doesnt 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>

 

Strange RadioButton group behavior with ToolBar

标签:style   blog   http   io   ar   color   os   sp   for   

原文地址:http://www.cnblogs.com/3Tai/p/4156128.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!