我已经为嵌套在数据网格中的 Listbox 定制了我的 ListBoxItem。但是,当我尝试遍历数据网格以查找列表框的控件时,但当我尝试获取正在选择的单选按钮的值时它失败了。
任何人请就任何可能有帮助的方法或解决方案片段提出建议?非常感谢。
<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();
}