在WPF中获取datagrid的datagridrow

时间:2012-06-29 05:37:53

标签: wpf wpfdatagrid

我在代码中使用了这一行,但它没有给出行对象,row为null。

DataGridRow row = 
   (DataGridRow)dtgSensorReadingList.ItemContainerGenerator.ContainerFromItem(channelGrid.Items[i]); 

Datagrid Xaml代码:

<DataGrid Visibility="Hidden" VirtualizingStackPanel.VirtualizationMode="Standard" 
    CanUserAddRows="False" ColumnHeaderHeight="32" MinColumnWidth="65" 
    HorizontalGridLinesBrush="DarkKhaki" VerticalGridLinesBrush="DarkKhaki" 
    BorderBrush="DarkKhaki" Block.TextAlignment="Center" AutoGenerateColumns="True" 
    CanUserResizeColumns="False" CanUserReorderColumns="False" HorizontalAlignment="Left" 
    Margin="63,540,0,0" Name="dtgSensorReadingList" ItemsSource="{Binding}" Grid.Row="1" 
    Height="Auto" VerticalAlignment="Top" MaxWidth="1920" Width="Auto">
    <DataGrid.Resources>
        <Style TargetType="{x:Type DataGridColumnHeader}">
            <Setter Property="Background" Value="SteelBlue" />
            <Setter Property="Foreground" Value="white" />
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
        </Style>
        <Style TargetType="{x:Type DataGridRowHeader}">
            <Setter Property="Background" Value="Transparent" />
        </Style>
        <Style x:Key="Body_Content_DataGrid_Centering" TargetType="{x:Type DataGridCell}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type DataGridCell}">
                        <Grid Background="{TemplateBinding Background}">
                            <ContentPresenter VerticalAlignment="Center" />
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </DataGrid.Resources>
</DataGrid> 

请告诉我如何获取DatGridRow?

1 个答案:

答案 0 :(得分:0)

如果您想要所选行,您可以绑定到数据网格中的SelectedItem。

请告诉我们你想要达到的目标。