C# 无法读取Datagrid WPF中嵌套的自定义列表框值

C# 无法读取Datagrid WPF中嵌套的自定义列表框值,c#,wpf,datagrid,listbox,wpf-controls,C#,Wpf,Datagrid,Listbox,Wpf Controls,我已经为Listbox定制了我的ListBoxItem,它嵌套在datagrid中。但是,当我尝试在datagrid中循环查找listbox的控件时,当我尝试获取正在选择的单选按钮的值时,它失败了 有没有人能提供一些可能有用的方法或解决方案的建议?非常感谢 <Page.Resources> <Style x:Key="RadioButtonItemStyle" TargetType="{x:Type ListBoxItem}"> <Sette

我已经为Listbox定制了我的ListBoxItem,它嵌套在datagrid中。但是,当我尝试在datagrid中循环查找listbox的控件时,当我尝试获取正在选择的单选按钮的值时,它失败了

有没有人能提供一些可能有用的方法或解决方案的建议?非常感谢

<Page.Resources>
    <Style x:Key="RadioButtonItemStyle" TargetType="{x:Type ListBoxItem}">
        <Setter Property="Margin" Value="0,0,5,0" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListBoxItem}">
                    <Border BorderThickness="0" Background="Transparent">
                        <!-- Note: IsChecked is bound to IsSelected-->
                        <RadioButton 
                    Focusable="False" 
                    IsHitTestVisible="False" 
                    IsChecked="{TemplateBinding IsSelected}">
                            <ContentPresenter />
                        </RadioButton>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <ItemsPanelTemplate x:Key="HorizontalItemsPanel">
        <VirtualizingStackPanel 
    Orientation="Horizontal" />
    </ItemsPanelTemplate>
</Page.Resources>
<Grid>
    <StackPanel>
        <StackPanel Orientation="Horizontal">
            <Label Name="GroupQuestionHeader" FontSize="14" FontWeight="Bold" FontFamily="Times New Roman" />
            <Label Name="PageCount" FontSize="10" FontFamily="Times New Roman"></Label>
        </StackPanel>

        <DataGrid AutoGenerateColumns="False" HorizontalAlignment="Left" Name="dataGrid1" VerticalAlignment="Top" CanUserReorderColumns="False" CanUserSortColumns="False" CanUserResizeColumns="False" CanUserAddRows="False">
            <DataGrid.CellStyle>
                <Style TargetType="DataGridCell">
                    <Setter Property="Padding" Value="5" />
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type DataGridCell}">
                                <Border Padding="{TemplateBinding Padding}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                                    <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                                </Border>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                    <Style.Triggers>
                        <Trigger Property="IsSelected" Value="true">
                            <Setter Property="Background" Value="Transparent" />
                            <Setter Property="Foreground" Value="Black" />
                            <Setter Property="BorderBrush" Value="{x:Null}" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </DataGrid.CellStyle>
            <DataGrid.Columns>
                <DataGridTemplateColumn Header="Question" Width="400">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock TextWrapping="Wrap" Text="{Binding QuestionContent, Mode=OneWay}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="We fully Comply | We partly Comply | We do not Comply" Width="*">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ListBox 
                            BorderThickness="0" 
                            SelectedValue="{Binding MyDataListSelectedValue}" 
                            ItemContainerStyle="{StaticResource RadioButtonItemStyle}" 
                            ItemsPanel="{StaticResource HorizontalItemsPanel}" Name="OptionsRadioButtonGroup" HorizontalContentAlignment="Left"
                                Cursor="Hand" HorizontalAlignment="Left">
                                <ListBoxItem Width="90"/>
                                <ListBoxItem Width="90"/>
                                <ListBoxItem/>
                            </ListBox>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
        <Button Content="Next Page" Height="23" HorizontalAlignment="Right" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
    </StackPanel>
</Grid>

我使用的方法

for (int i=0;i<dataGrid1.Items.Count;i++) 
{ 
    DataRowView datarowv = (DataRowView)dataGrid1.Items[i]; 
    DataRow dr = datarowv.Row; 
    string RBValue = dr.ItemArray[1].toString(); 
}

for(int i=0;i这个怎么样。我没有编译代码,我只是直接从头脑中写出来的。如果出现编译错误,很抱歉。请修复它们

for (int i=0;i<dataGrid1.Items.Count;i++) 
{

    // get control which represents the data
    var control = dataGrid1.ItemContainerGenerator.ContainerFromItem(dataGrid1.Items[i]);
    DependencyObject obj = control

    // inside that control there is somewhere the ListBox so run down the visualtree till you find the damn ListBox
    for(;!(obj is ListBox);
        obj = VisualTreeHelper.GetChild(obj, 0));

    ListBox listBox = obj as ListBox;
    if(listBox != null)
    {
      // get the selected values from ListBox
      var selectedItems = listBox.SelectedItems;
      foreach(var selectedItem in selecteditems)
      {
         Console.WriteLine("I am a selected item: " + selectedItem.ToString());
      }
    } 
}

for(int i=0;i如何尝试获取单选按钮的值?您好@hedgehog,谢谢您的回复。
**var control=ItemsControl.containerformelement(dataGrid1,dataGrid1.Items[i]);***
在我试图编译时,上面的行抛出了一个错误。我试图将其转换为“(DataGridRow)dataGrid1.Items[i],虽然VS没有编译错误,但当我尝试检索该值时,VS抛出并且异常无法将类型为“POCSurveySystem.Business.Entities.Question”的对象强制转换为类型为“System.Windows.Controls.DataGridRow”。很抱歉,我是WPF新手,非常感谢您的帮助。您好,我不确定您在说什么。但是查看MSDN:ContainerRomeElement返回属于拥有给定元素的当前ItemsControl的容器。如果有人有任何其他方法,请与我分享。同时,我将继续努力使hedgehog的建议生效(但目前无法编译)谢谢。我编辑了我的答案。使用这个:var control=dataGrid1.ItemContainerGenerator.ContainerFromItem(dataGrid1.Items[I]);谢谢@hedgehog:)干杯!