Datagrid scroll bar stops working

2019-07-22 04:52发布

问题:

When I run the app, the vertical scroll bar works as expected. However, when I add a new line/row, the bar (control that should go up and down on the slider) doesn't slide. With the mouse wheel I can scroll up and down the list of rows, and I can click on the up and down arrows. So the scroll bar works, but not as expected. The control should slide up and down, like it does at first, but after adding that new line, it does not.

I hope that is clear enough, I've searched many issues to find this peculiar behavior, but was unsuccessful. Here is the XAML, in part, as it is now:

<DataGrid x:Name="inventoryDataGrid" AutoGenerateColumns="False" 
  SelectedValuePath="Id"
  EnableRowVirtualization="True"               
  EnableColumnVirtualization="True" 
  Style="{DynamicResource DataGridDemoStyle}"
  CanUserSortColumns="True"
  VerticalAlignment="Top" 
  ItemsSource="{Binding Source={StaticResource claimInventoryViewSource}}" 
  RowEditEnding="dgInv_RowEditEnding"  
  CellEditEnding="dgInv_CellEditEnding"
  SelectionChanged="dgInv_SelectionChanged"                                             
  IsSynchronizedWithCurrentItem="True"  CanUserAddRows="False" 
  RowHeaderWidth="0"
  Sorting="DataGrid_Standard_Sorting" MouseDoubleClick="inventoryDataGrid_DoubleClick"
  CanUserDeleteRows="True"
  SelectionMode="Single"
  HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Visible"
  Width="999.5"
  CommandManager.PreviewCanExecute="Grid_PreviewCanExecute" Grid.Column="0"
  Grid.Row="1"   
  Margin="0,3,0,0" RowDetailsVisibilityMode="VisibleWhenSelected" Height="227"     
  LostFocus="inventoryDataGrid_LostFocus" Background="#FFFCF2E7"   
  AlternatingRowBackground="#FFF2F2D6" RowBackground="#FF6FC4BF"
  GotFocus="inventoryDataGrid_GotFocus">
<DataGrid.Resources>
 <Style x:Key="DataGridCellStyle" TargetType="{x:Type DataGridCell}" >
  <Style.Triggers>
   <Trigger Property="IsSelected" Value="True">
    <Setter Property="Foreground" Value="White"/>
   </Trigger>
  </Style.Triggers>
 </Style>
</DataGrid.Resources>
<DataGrid.Columns>

Thanks!

回答1:

I was able to solve this issue. The problem was that there was code I implemented a long time ago, in an EndEdit routine (found here: EndEdit equivalent in WPF), which somehow caused this erratic behavior in my datagrid scrollbar.

Once I removed this code, my scrollbar worked without problems. Then of course I had to research a way to save the data in text boxes without the use of EndEdit, but that is way off topic for this Question.