我为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();
}
解决方法:
这个怎么样.我没有编译我直接从头脑中编写的代码.对不起,如果你遇到编译错误.只需修复它们.
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());
}
}
}
如果您不明白这个小代码在做什么,请问.