在代码隐藏中找到DataTemplate中的WPF元素

时间:2015-04-19 06:28:52

标签: c# wpf element datatemplate

这是我的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>

单击SubmitButton时我想要做的是(Code Behind):

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')
    }
}

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

0 个答案:

没有答案