我使用过Datagrid并订阅了ScrollViewer.ScrollChanged事件。 Datagrid的Item源是一个简单的数据表(GDItems)。 还有另一个数据表(GDAllItems),它保存从数据库中获取的实际数据集。
逻辑是这样写的:
现在主要的问题是ScrollViewer.ScrollChanged事件被连续触发并且调试器没有出现。
xaml中的大多数列都是“DataGridTextColumn”,除了两列设置为“DataGridTemplateColumn.CellTemplate”,因为其中需要可编辑的“TextBox”。 如果删除这两列或将其类型更改为“DataGridTextColumn”,则代码将完美运行(ScrollViewer.ScrollChanged事件仅触发一次且不连续)。
我无法弄清楚使用“DataGridTemplateColumn.CellTemplate”与持续发生的事件之间的联系。
帮助感谢!
XAML代码:
<DataGrid Name="gridFinalResult" AutoGenerateColumns="False"
ItemsSource="{Binding GDFinalResult,IsAsync=True}"
SelectedItem="{Binding SelectedItemFinalResult}" RowDetailsVisibilityMode="VisibleWhenSelected" CanUserSortColumns="True" IsSynchronizedWithCurrentItem="False" TextBlock.TextAlignment="Center" FrozenColumnCount="1" VirtualizingStackPanel.VirtualizationMode="Standard" SelectionMode="Single" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" CanUserResizeRows="True" CanUserReorderColumns="True" HorizontalAlignment="Stretch" Width="Auto" ScrollViewer.CanContentScroll="False" Grid.Row="0" VerticalAlignment="Stretch" HeadersVisibility="Column" RowHeight="NaN" EnableColumnVirtualization="True" EnableRowVirtualization="True">
<DataGridTextColumn Binding="{Binding CompanyNo}" Header="Company
(#)"
Width="60"/>
<DataGridTextColumn Binding="{Binding CompanyName}" Header="Company
Name"
Width="110"/>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate> <TextBox properties:Tracker.Property="Text" KeyboardNavigation.TabIndex="54"
Name="txtNewQty" Numeric:ZeroNumericMaskBehavior.Mask="NonNegativeNumber" MaxLength="6" Text="{Binding Path=FinalRecommendedQty, StringFormat='{}{0:#,0;}', Mode=TwoWay}"
LostFocus="txtNewQty_LostFocus" TextChanged="txtNewQty_TextChanged"
GotFocus="txtNewQty_GotFocus" IsTabStop="True" KeyboardNavigation.TabNavigation="Continue">
</TextBox> </DataTemplate></DataGridTemplateColumn.CellTemplate>
滚动更改的代码:
private void gridFinalResult_ScrollChanged(object sender, ScrollChangedEventArgs e)
{
var scrollViewer = VisualTreeHelpers.FindChild<ScrollViewer>(gridFinalResult);
if (rsoVM.GDFinalResult != null && rsoVM.GDFinalDBResult != null)
{
if (rsoVM.GDFinalDBResult.Rows.Count.ToString() == rsoVM.GDFinalResult.Rows.Count.ToString())
{
return;
}
}
if (scrollViewer.VerticalOffset > 0 && scrollViewer.ScrollableHeight > 0)
{
if (scrollViewer.VerticalOffset == scrollViewer.ScrollableHeight)
{
DataTable dt = new DataTable("dt");
dt = rsoVM.GDFinalResult.Copy();
object getSelecteditem = null;
if (gridFinalResult.SelectedItem != null)
getSelecteditem = gridFinalResult.SelectedItem;
DataRow[] drRSOList = rsoVM.GDFinalResult.Select("RowID = MAX(RowID)");
if (drRSOList.Length > 0)
{
int i = 0;
for(Int32 j = Convert.ToInt32(drRSOList[0]["RowID"].ToString()) + 1 ; j <= Convert.ToInt32(drRSOList[0]["RowID"].ToString()) + 15; j++)
{
dt.ImportRow(rsoVM.GDFinalDBResult.Rows[j]);
}
Dispatcher.BeginInvoke(new Action(() =>
{
rsoVM.GDFinalResult = dt;
rsoVM.OnPropertyChanged("GDFinalResult");
}), System.Windows.Threading.DispatcherPriority.ContextIdle);
Dispatcher.BeginInvoke(new Action(() =>
{
scrollViewer.ScrollToVerticalOffset(scrollViewer.ScrollableHeight / 2);
if (getSelecteditem != null)
{
long ID = long.Parse(((System.Data.DataRowView)(getSelecteditem)).Row.ItemArray[0].ToString());
int index = CommonClass.GetIndex(rsoVM.GDFinalResult, ID, "RowID");
if (index != -1)
{
gridFinalResult.SelectedItem = getSelecteditem;
DataGridRow dgrow = (DataGridRow)gridFinalResult.ItemContainerGenerator.ContainerFromItem(gridFinalResult.Items[index]);
if (dgrow != null)
dgrow.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
}
}
}),
System.Windows.Threading.DispatcherPriority.ContextIdle);
}
}
}
}