DataGrid SelectedItems.Count为0,直到第二次单击

时间:2014-01-31 09:58:26

标签: c# wpf datagrid

我有一个像这样定义的DataGrid和PreviewMouseLeftButtonDown事件:

if (InputDocItemsDataGrid.SelectedItems.Count == 1)
        {
            foreach (CheckBox checkBox in VisualTreeHelpers.FindVisualChildren<CheckBox>(InputDocItemsDataGrid, "cbxSingleRow"))
            {
                var row = VisualTreeHelpers.FindAncestor<DataGridRow>(checkBox);
                if (row.IsSelected == true)
                {
                    checkBox.IsChecked = true;
                }
                else
                {
                    checkBox.IsChecked = false;
                }
            }
        }

当我点击某一行时,SelectedItems.Count为0,如果我再次点击同一行,那么SelectedItems.Count为1.我不明白为什么会发生这种情况因为我的数据网格也有SelectionChanged事件定义如下:

private void InputDocItemsDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        (this.DataContext as InputDocsViewModel).SelectedInputItems = new List<int>();
        foreach (tblInputDocItem item in InputDocItemsDataGrid.SelectedItems)
        {
            (this.DataContext as InputDocsViewModel).SelectedInputItems.Add(item.InputDocItemID);
        }
        if ((this.DataContext as InputDocsViewModel).SelectedInputItems.Count > 1)
        {
            (this.DataContext as InputDocsViewModel).IsEditEnabled = false;
        }
    }

在这种情况下,一切正常,当我点击datagrid中的某一行时,我不必点击两次,我的SelectedItems.Count立即为1.

这是我的datagrid:

 <DataGrid AutoGenerateColumns="False"
                                      IsReadOnly="True"
                                      Name="InputDocItemsDataGrid"
                                      ItemsSource="{Binding Path= InputItems}" 
                                      SelectedItem="{Binding Path= InputItem, UpdateSourceTrigger=PropertyChanged}"
                                      SelectionChanged="InputDocItemsDataGrid_SelectionChanged"
                                      PreviewMouseLeftButtonDown="InputDocItemsDataGrid_PreviewMouseLeftButtonDown">
                            <DataGrid.Columns>
                                <DataGridTemplateColumn CanUserReorder="False" CanUserResize="False">
                                    <DataGridTemplateColumn.HeaderTemplate>
                                        <DataTemplate>
                                            <CheckBox Name="cbxAll" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center" Checked="cbxAll_Checked" PreviewMouseLeftButtonDown="cbxAll_PreviewMouseLeftButtonDown" />
                                        </DataTemplate>
                                    </DataGridTemplateColumn.HeaderTemplate>
                                    <DataGridTemplateColumn.CellTemplate>
                                        <DataTemplate>
                                            <CheckBox Name="cbxSingleRow" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center" PreviewMouseLeftButtonDown="cbxSingleRow_PreviewMouseLeftButtonDown" />
                                        </DataTemplate>
                                    </DataGridTemplateColumn.CellTemplate>
                                </DataGridTemplateColumn>
                                <DataGridTextColumn Header="{DynamicResource StockStr}" Binding="{Binding Path= tblInputDoc.tblStock.Name}"  />
                                    <DataGridTextColumn Header="{DynamicResource UCodeStr}" Binding="{Binding Path= tblGood.UCode}" />
                                    <DataGridTextColumn Header="{DynamicResource GoodStr}" Binding="{Binding Path= tblGood.Name}" />
                                    <DataGridTextColumn Header="{DynamicResource AmmountStr}" Binding="{Binding Path= Amount, Converter={StaticResource moneyConverter}}" />
                                    <DataGridTextColumn Header="{DynamicResource InputPriceStr}" Binding="{Binding Path= InputPrice, Converter={StaticResource moneyConverter}}" />
                                    <DataGridTextColumn Header="{DynamicResource SuggestedPriceStr}" Binding="{Binding Path= SuggestedPrice, Converter={StaticResource moneyConverter}}" />
                                    <DataGridTextColumn Header="{DynamicResource InputValueStr}" Binding="{Binding Path= InputValue, Mode=OneWay, Converter={StaticResource moneyConverter}}" />
                                    <DataGridTextColumn Header="{DynamicResource VatBaseStr}" Binding="{Binding Path= VatBase, Mode=OneWay, Converter={StaticResource moneyConverter}}" />
                                    <DataGridTextColumn Header="{DynamicResource VatValueStr}" Binding="{Binding Path= VatValue, Mode=OneWay, Converter={StaticResource moneyConverter}}" />
                                    <DataGridTextColumn Header="{DynamicResource InputWithVatStr}" Binding="{Binding Path= InputPriceWithVat, Mode=OneWay, Converter={StaticResource moneyConverter}}" />


                                </DataGrid.Columns>
                            </DataGrid>

有谁知道为什么会这样?

0 个答案:

没有答案