The easiest way is to give each RadioButton a name and check its IsChecked property.
<RadioButton x:Name="RadioButtonA" Content="A" GroupName="myGroup"></RadioButton> <RadioButton x:Name="RadioButtonB" Content="B" GroupName="myGroup"></RadioButton> <RadioButton x:Name="RadioButtonC" Content="C" GroupName="myGroup"></RadioButton> if (RadioButtonA.IsChecked) { ... } else if (RadioButtonB.IsChecked) { ... } else if (RadioButtonC.IsChecked) { ... }
But using Linq and a logical tree, you can make this a little less verbose:
myWindow.FindDescendants<CheckBox>(e => e.IsChecked).FirstOrDefault();
Where FindDescendants is a reusable method:
public static IEnumerable<T> FindDescendants<T>(this DependencyObject parent, Func<T, bool> predicate, bool deepSearch = false) where T : DependencyObject { var children = LogicalTreeHelper.GetChildren(parent).OfType<DependencyObject>().ToList(); foreach (var child in children) { var typedChild = child as T; if ((typedChild != null) && (predicate == null || predicate.Invoke(typedChild))) { yield return typedChild; if (deepSearch) foreach (var foundDescendant in FindDescendants(child, predicate, true)) yield return foundDescendant; } else { foreach (var foundDescendant in FindDescendants(child, predicate, deepSearch)) yield return foundDescendant; } } yield break; }
source share