为什么函数GetRow返回null?

时间:2014-03-06 12:35:21

标签: c# wpf datagrid

我想获取DataGrid的第一行,但变量行返回null,我不知道为什么? 使用GetRow的方法:

public static DataGridRow GetRow(this DataGrid grid, int index)
{
    DataGridRow row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
    if (row == null)
    {
        // May be virtualized, bring into view and try again.

        grid.UpdateLayout();
        grid.ScrollIntoView(grid.Items[index]);
        row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);

    }
    return row;
}

 public void kraj_wybierz(Object obj)
 {
     DataGrid dg = obj as DataGrid; //dg has got 11 Items
     var row = dg.GetRow(0);
 }

XAML:

<StackPanel x:Name="WszystkieZdarzeniaStackPanel2">
    <DataGrid xmlns:local="clr-namespace:AllSportsBets102"
                x:Name="ZdarzeniaDataGrid"
                TabIndex="7"
                IsReadOnly="False"
                HeadersVisibility="Column"
                SelectionUnit="FullRow"
                CanUserAddRows="False"
                AutoGenerateColumns="False"
                VerticalAlignment="Top"
                Height="383"
                Background="White"
                BorderBrush="Transparent"
                BorderThickness="2"
                CellEditEnding="ZdarzeniaDataGrid_CellEditEnding">
        <DataGrid.Columns>
            <DataGridComboBoxColumn x:Name="DGKontynent"
                                    SelectedValueBinding="{Binding Kontynent}"
                                    Header="Kontynent"
                                    Width="130"
                                    IsReadOnly="False" />
            <DataGridComboBoxColumn x:Name="DGKraj"
                                    SelectedValueBinding="{Binding Kraj, Mode=TwoWay}"
                                    Header="Kraj"
                                    Width="130"
                                    IsReadOnly="False" />
        </DataGrid.Columns>
    </DataGrid>
</StackPanel>

0 个答案:

没有答案