Original address: 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 scenarioinchWhich those radiobuttons belonging to the same group might resideinchdifferent visual tree. This is trueWhen used inside ToolBar, because those content displayedinchThe Over-flow section of ToolBar is actually residingincha Popup, the following code shows a possible workaround:code BlockusingSystem;usingSystem.Windows;usingSystem.Windows.Media;usingSystem.Windows.Controls;usingSystem.Windows.Controls.Primitives;namespacesheva.windows.controls{ Public classWorkaroundtoolbar:toolbar {protected Override voidPreparecontainerforitemoverride (DependencyObject element, Object item) {Base. Preparecontainerforitemoverride (element, item); Workaroundradiobutton RadioButton= Element asWorkaroundradiobutton; if(RadioButton! =NULL) {Radiobutton.setvalue (Defaultstylekeyproperty, Toolbar.radiobuttonstylekey); } } } Public classWorkaroundradiobutton:radiobutton {protected Override voidonchecked (RoutedEventArgs e) {Boolean bypassbuildinunchecklogic=false; DependencyObject Parent=Base. Parent; if(Parent! =NULL) { foreach(DependencyObject LogicalchildinchLogicaltreehelper.getchildren (parent)) { //If There is a logical child connected to the popup, we should employ our own invention to uncheck the group. if(Getpopupfromvisualchild (Logicalchild asVisual)! =NULL) {bypassbuildinunchecklogic=true; } } if(bypassbuildinunchecklogic) {foreach(DependencyObject LogicalchildinchLogicaltreehelper.getchildren (parent)) {Workaroundradiobutton RadioButton= Logicalchild asWorkaroundradiobutton; if(RadioButton! = This&&! String.IsNullOrEmpty (radiobutton.groupname) && Radiobutton.groupname = =Base. GroupName) {radiobutton.ischecked=false; } } } } if(!bypassbuildinunchecklogic) { Base. Onchecked (e); } } Private StaticPopup Getpopupfromvisualchild (Visual child) {visual parent=Child ; FrameworkElement Visualroot=NULL; while(Parent! =NULL) {Visualroot= Parent asFrameworkElement; Parent= Visualtreehelper.getparent (parent) asVisual; } Popup Popup=NULL; if(Visualroot! =NULL) {Popup= Visualroot.parent asPopup; } returnpopup; } }}<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=" -"Width=" -"> <DockPanel> <cc:workaroundtoolbar height=" -"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=" -"height=" -"Name="btn"/> </DockPanel></Window>
Strange RadioButton group Behavior with ToolBar