Wpf 如何获取对一组单选按钮的引用并找到所选的单选按钮?

Wpf 如何获取对一组单选按钮的引用并找到所选的单选按钮?,wpf,xaml,radio-button,selecteditem,Wpf,Xaml,Radio Button,Selecteditem,使用以下XAML如何在按钮的eventhandler中获取对所选radiobutton的引用 <Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Mai

使用以下XAML如何在按钮的eventhandler中获取对所选radiobutton的引用

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" x:Name="myWindow">
    <Grid>
        <StackPanel>
            <RadioButton Content="A" GroupName="myGroup"></RadioButton>
            <RadioButton Content="B" GroupName="myGroup"></RadioButton>
            <RadioButton Content="C" GroupName="myGroup"></RadioButton>
        </StackPanel>
        <Button Click="Button_Click" Height="100" Width="100"></Button>
    </Grid>
</Window>


您可以使用
列表框
,如中所示,其工作原理是将项目模板化为绑定到
列表框项目
IsSelected
单选按钮
,然后将
列表框绑定到属性。

最简单的方法是为每个单选按钮指定一个名称,并测试了它的IsChecked性能

<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) {
    ...
}

如果(RadioButtonA.IsChecked){
...
}否则,如果(RadioButtonB.已检查){
...
}否则如果(RadioButtonC.已检查){
...
}
但是,使用Linq和逻辑树,您可以使其不那么冗长:

myWindow.FindDescendants<CheckBox>(e => e.IsChecked).FirstOrDefault();
myWindow.finddescents(e=>e.IsChecked).FirstOrDefault();
其中FindDescents是一种可重用的扩展方法:

    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;
    }
公共静态IEnumerable FindDescents(此DependencyObject父对象,Func谓词,bool deepSearch=false),其中T:DependencyObject{
var children=logicaltreeheloper.GetChildren(parent).OfType().ToList();
foreach(儿童中的儿童变量){
var typedChild=子项为T;
if((typedChild!=null)和&(predicate==null | | predicate.Invoke(typedChild))){
收益型儿童;
if(deepSearch)foreach(finddescents中的var foundgroundant(child,predicate,true))产生返回foundgroundant;
}否则{
foreach(findDescents(子级、谓词、深度搜索)中的var foundground)产生返回foundground;
}
}
屈服断裂;
}

如果您知道您的容器ID,那么与公认的答案相比,您的答案就不那么毛茸茸了:

var radioButtons = LogicalTreeHelper.GetChildren(_myStackPanel).OfType<RadioButton>();
var selected = radioButtons.Where(x => (bool)x.IsChecked).FirstOrDefault();
var radioButtons=logicaltreeheloper.GetChildren(_myStackPanel).OfType();
var selected=单选按钮。其中(x=>(bool)x.IsChecked)。FirstOrDefault();

谢谢您的回答。我明白了[这个答案1]。可能是打字错误?