简体   繁体   中英

How to Prevent an Autoscroll in DataGrid

I have a datagrid defined in XAML as follows:

<toolkit:DataGrid Margin="10,116,62,34" Name="WADataGrid" RowBackground="LightYellow"   AlternatingRowBackground="White"
                          BorderBrush="Gray" BorderThickness="2" IsReadOnly="True"     CanUserReorderColumns="False" CanUserResizeColumns="True" 
                          CanUserSortColumns = "True" SelectionMode="Extended" MouseDoubleClick="DataGrid_MouseDoubleClick" 
                          AutoGenerateColumns="False" Height="400" Canvas.Left="0" Canvas.Top="-76" Width="731">

I then process the item selected by the row that the double click occurred.

What happens though when the vertical scrollbar is on and there are items below the last row of the grid not yet displayed, the double click causes the last row to scroll up one so that it becomes the next to last row. The value of the selected item in the double click method is the row that had been hidden and got scrolled up.

How can I prevent the datagrid from scrolling up when the last row displayed is double clicked?

I was able to work around this by instead using PreviewMouseDoubleClick. When doing that I was able to identify the correct row before the scroll occurred.

I have achieved the same, which you wanted with the following code snippet. Set DataGrid attribute in Xaml ScrollViewer.CanContentScroll="False"

    <DataGrid ScrollViewer.CanContentScroll="False" ... />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM