<Grid Background="Transparent" Margin="0,22,0,23" Height="250"><DataGrid x:Name="ViewDataGrid" ItemsSource="{Binding ItemData, Mode=TwoWay}" Margin="0,0,0,10" RowStyle="{StaticResource DataGridRowStyle}" HeadersVisibility="None" AutoGenerateColumns="False" RowHeaderWidth="0" ClipToBounds="True" HorizontalScrollBarVisibility="Disabled"><i:Interaction.Behaviors><cls:ScrollIntoViewBehavior/></i:Interaction.Behaviors><DataGrid.Resources><SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="DarkGray"/><Style TargetType="DataGridCell"><Setter Property="ToolTip"><Setter.Value><StackPanel><TextBlock Text="{Binding Key}" /><TextBlock Text="{Binding Value}" /></StackPanel></Setter.Value></Setter><Setter Property="Foreground" Value="Black"/><Style.Triggers><DataTrigger Binding="{Binding IsNewRecord, Mode=TwoWay}" Value="True"><Setter Property="Foreground" Value="White"></Setter></DataTrigger></Style.Triggers></Style></DataGrid.Resources><DataGrid.Columns><DataGridTextColumn Binding="{Binding Key}" Width="152"/><DataGridTextColumn Binding="{Binding Value}" Width="153"/></DataGrid.Columns></DataGrid></Grid>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Interactivity;
namespace xxx.Client.Presentation
{
public class ScrollIntoViewBehavior : Behavior<DataGrid>
{
public static object _lastProduct { get; set; }
public static int _lastIndex { get; set; }
public static int _gridItemsCount { get; set; }
public static int _gridItemsNewCount { get; set; }
protected override void OnAttached()
{
base.OnAttached();
this.AssociatedObject.SelectionChanged += DataGrid_SelectionChanged;
this.AssociatedObject.Loaded += AssociatedObject_Loaded;
}
void AssociatedObject_Loaded(object sender, System.Windows.RoutedEventArgs e)
{
object currentPos = _lastProduct;
if (currentPos != null)
{
if (sender is DataGrid)
{
DataGrid datagrid = sender as DataGrid;
datagrid.CurrentColumn = datagrid.Columns[0];
_gridItemsNewCount = datagrid.Items.Count;
if (_gridItemsCount == _gridItemsNewCount)
{
datagrid.Dispatcher.BeginInvoke((Action)(() =>
{
datagrid.ScrollIntoView(datagrid.Items[_lastIndex], datagrid.Columns[0]);
datagrid.UpdateLayout();
}));
}
else
{
datagrid.Dispatcher.BeginInvoke((Action)(() =>
{
datagrid.ScrollIntoView(datagrid.Items[_gridItemsNewCount-1], datagrid.Columns[0]);
datagrid.UpdateLayout();
}));
}
}
}
}
protected override void OnDetaching()
{
base.OnDetaching();
this.AssociatedObject.SelectionChanged -= DataGrid_SelectionChanged;
}
void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (sender is DataGrid)
{
DataGrid datagrid = (sender as DataGrid);
if (datagrid.SelectedItem != null)
{
datagrid.Dispatcher.BeginInvoke((Action)(() =>
{
if (datagrid.SelectedItem != null)
{
// if (lastProduct != null)
{
_lastProduct = datagrid.CurrentItem as object;
_lastIndex = datagrid.SelectedIndex;
_gridItemsCount = datagrid.Items.Count;
}
if (_lastProduct != null)
{
datagrid.UpdateLayout();
datagrid.ScrollIntoView(datagrid.SelectedItem);
}
else
{
datagrid.UpdateLayout();
datagrid.ScrollIntoView(datagrid.SelectedItem);
}
}
}));
}
}
}
}
}