DataGrid的原因ItemContainerGenerator.ContainerFromIndex的Row返回null

时间:2014-02-27 07:27:05

标签: c# wpf datagrid itemcontainergenerator

我打算继续专注于DataGrid的Selected Row。为此,我搜索并找到链接How do I set the focus on a datagrid to a specific row? ..当我在我的项目中尝试相同时,我发现该行总是返回null ...

我也为此瞪眼了,得到了link...这里我没有使用虚拟化,更多的是我已经关闭虚拟化..仍然面临同样的..我可以知道为什么这会返回null的一些原因?

<DataGrid Name="DGrid"
                  Grid.Row="0"
                  Grid.Column="0"
                  HorizontalAlignment="Stretch"
                  AutoGenerateColumns="False"
                  BorderThickness="0"
                  CanUserAddRows="False"
                  CanUserDeleteRows="False"
                  CanUserResizeColumns="False"
                  CanUserSortColumns="True"
                  HeadersVisibility="Column"
                  IsReadOnly="True"
                  ItemsSource="{Binding Path=Reports,
                                        NotifyOnSourceUpdated=True}"
                  MouseDoubleClick="DGrid_MouseDoubleClick"
                  PreviewKeyDown="DGrid_PreviewKeyDown"
                  SelectionChanged="DGrid_SelectionChanged"
                  SelectionMode="Extended"
                  SelectionUnit="FullRow"
                  VerticalScrollBarVisibility="Auto" VirtualizingStackPanel.IsVirtualizing="False">
            <DataGrid.Resources>
                <Style x:Key="CellEditingStyle" TargetType="DataGridCell">
                    <Setter Property="DataGridCell.IsTabStop" Value="False" />
                </Style>
            </DataGrid.Resources>
            <DataGrid.Columns>
                <DataGridTemplateColumn x:Name="DueStatus"
                                        Width="16"
                                        CellStyle="{StaticResource CellEditingStyle}"
                                        SortMemberPath="OverDue">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <!--  ReSharper disable Xaml.BindingWithContextNotResolved - This will be generated at Runtime  -->
                                <Image Name="ImgOvr"
                                       Width="16"
                                       Height="16"
                                       Source="/VersadexMreUs;component/Images/signal_flag_red.png"
                                       Visibility="{Binding Converter={StaticResource TextToVisiblity},
                                                            Path=OverDue}" />
                                <!--  ReSharper restore Xaml.BindingWithContextNotResolved  -->
                            </StackPanel>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn x:Name="ArrestStatus"
                                        Width="16"
                                        CellStyle="{StaticResource CellEditingStyle}"
                                        SortMemberPath="ArrestMade">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <!--  ReSharper disable Xaml.BindingWithContextNotResolved - This will be generated at Runtime  -->
                                <Image Width="16"
                                       Height="16"
                                       Source="/VersadexMreUs;component/Images/car_compact_red.png"
                                       Visibility="{Binding Converter={StaticResource TextToVisiblity},
                                                            Path=ArrestMade}" />
                                <!--  ReSharper restore Xaml.BindingWithContextNotResolved  -->
                            </StackPanel>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn x:Name="IncompleteStatus"
                                        Width="16"
                                        CellStyle="{StaticResource CellEditingStyle}"
                                        SortMemberPath="InComplete">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <!--  ReSharper disable Xaml.BindingWithContextNotResolved - This will be generated at Runtime  -->
                                <Image Width="16"
                                       Height="16"
                                       Source="/VersadexMreUs;component/Images/document_warning.png"
                                       Visibility="{Binding Converter={StaticResource TextToVisiblity},
                                                            Path=InComplete}" />
                                <!--  ReSharper restore Xaml.BindingWithContextNotResolved  -->
                            </StackPanel>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTextColumn x:Name="RptStatus"
                                    MinWidth="120"
                                    Binding="{Binding Path=StatusMessage,
                                                      Converter={StaticResource TextToUpper}}"
                                    Header="Status" />
                <DataGridTextColumn x:Name="RptDesc"
                                    MinWidth="200"
                                    Binding="{Binding Path=Description,
                                                      Converter={StaticResource TextToUpper}}"
                                    Header="Report Description" />
                <DataGridTextColumn x:Name="RptState"
                                    MinWidth="250"
                                    Binding="{Binding Path=CurrentState,
                                                      Converter={StaticResource TextToUpper}}"
                                    Header="Current State" />
                <DataGridTextColumn x:Name="RptOfficer"
                                    MinWidth="180"
                                    Binding="{Binding Path=OfficerName,
                                                      Converter={StaticResource TextToUpper}}"
                                    Header="Reporting Officer" />
                <DataGridTextColumn x:Name="RptDate"
                                    MinWidth="83"
                                    Binding="{Binding Path=SubmitDate,
                                                      Converter={StaticResource TextToUpper}}"
                                    Header="Submit Date" />
            </DataGrid.Columns>
        </DataGrid>

0 个答案:

没有答案