Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在代码隐藏中的DataTemplate内查找WPF元素_C#_Wpf_Element_Datatemplate - Fatal编程技术网

C# 在代码隐藏中的DataTemplate内查找WPF元素

C# 在代码隐藏中的DataTemplate内查找WPF元素,c#,wpf,element,datatemplate,C#,Wpf,Element,Datatemplate,这是我的XAML <ItemsControl Name="itmCntrl" > <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <WrapPanel Orientation="Horizontal" ></WrapPanel> </ItemsPanelTemplate> </ItemsControl.

这是我的XAML

<ItemsControl Name="itmCntrl" >
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel Orientation="Horizontal" ></WrapPanel>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <StackPanel Name="stk">
                <StackPanel Margin="5,2,0,0">
                    <WrapPanel Margin="10,10,10,5" Height="Auto">
                        <TextBlock x:Name="tbNI" FontSize="18" FontFamily="Times New Roman" Text="{Binding NUMINDEX}"/>
                        <TextBlock FontSize="18" FontFamily="Times New Roman" Text="{Binding QUESTION}"/>                                              
                    </WrapPanel>
                    <WrapPanel Margin="10,5,10,5" Height="Auto">
                        <RadioButton x:Name="rdoA" GroupName="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}, Path=A}"/>
                        <TextBlock TextWrapping="Wrap" Name="tbA" Text="{Binding A}" FontSize="15" FontFamily="Times New Roman" FontWeight="Bold" VerticalAlignment="Center" Foreground="Black" Margin="3,0,0,0"/>
                        <TextBlock Width="1000"/>
                        <RadioButton x:Name="rdoB" Margin="0,0,0,0" GroupName="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}, Path=B}"/>
                        <TextBlock TextWrapping="Wrap" Name="tbB" Text="{Binding B}" FontSize="15" FontFamily="Times New Roman" FontWeight="Bold" HorizontalAlignment="Right" VerticalAlignment="Center" Foreground="Black" Margin="3,0,0,0"/>
                        <TextBlock Width="1000"/>
                        <RadioButton x:Name="rdoC" GroupName="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}, Path=C}"/>
                        <TextBlock TextWrapping="Wrap" Name="tbC" Text="{Binding C}" FontSize="15" FontFamily="Times New Roman" FontWeight="Bold" HorizontalAlignment="Left" VerticalAlignment="Center" Foreground="Black" Margin="3,0,0,0"/>
                        <TextBlock Width="1000"/>
                        <RadioButton x:Name="rdoD" GroupName="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}, Path=D}" Margin="0,0,0,0"/>
                        <TextBlock TextWrapping="Wrap" Name="tbD" Text="{Binding D}" FontSize="15" FontFamily="Times New Roman" FontWeight="Bold" HorizontalAlignment="Right" VerticalAlignment="Center" Foreground="Black" Margin="3,0,0,0"/>
                    </WrapPanel>
                </StackPanel>
            </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

那么如何访问DataTemplate中的文本框和单选按钮呢?我知道我应该使用VisualTreeHelper,但我还是新手,无法访问它。任何人对此都有解决方案?

可能会对您有所帮助。正确的方法是为每个
RadioButton
值添加一个布尔属性,并将它们绑定到
RadioButton
IsChecked
属性。或者更简单,一个枚举属性,而不是四个
RadioButton
放置一个带有四项的
ListBox
,并将
SelectedIndex
绑定到模型。所以我应该向ListBox添加4个rdo+Textblock,而不是WrapPanel?你能给我一些密码吗
private void SubmitButton_Click(object sender, RoutedEventArgs e)
{                
    if(rdoA.IsChecked == true)
    {
        Add(tbNI.Text,'A')
    }
    else if(rdoB.IsChecked == true)
    {
        Add(tbNI.Text,'B')
    }
    else if(rdoC.IsChecked == true)
    {
        Add(tbNI.Text,'C')
    }
    else if(rdoD.IsChecked == true)
    {
        Add(tbNI.Text,'D')
    }
}