4

我已经为嵌套在数据网格中的 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(); 
}
4

1 回答 1

1

这个怎么样。我还没有编译我只是直接从脑海中编写的代码。抱歉,如果您遇到编译错误。只是修复它们。

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());
      }
    } 
}

如果你不明白这个小代码在做什么,请问。

于 2013-03-11T21:23:48.673 回答