WPF C#datagrid修复视图行(修复滚动) [英] WPF C# datagrid fix view row (fix scroll)

查看:70
本文介绍了WPF C#datagrid修复视图行(修复滚动)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用WPF数据网格,并希望在顶部插入行后修复视图,每当在顶视图中插入行向下滚动1行时,我使用以下代码

i am working on WPF datagrid and want to fix view after insert row at top, whenever row insert at top view scroll down by 1 row, i am using following code

int i = 0;
DataTable dt = null;

private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
    dt = new DataTable();
    dt.Columns.Add("Col1");
    dt.Columns.Add("Col2");

    DispatcherTimer dtimer = new DispatcherTimer();
    dtimer.Tick += new EventHandler(dt_Tick);
    dtimer.Interval = new TimeSpan(0, 0, 1);
    dtimer.Start();
    dataGrid1.DataContext = dt;


}
void dt_Tick(object sender, EventArgs e)
{
    DataRow dr;
    dr = dt.NewRow();
    dr["Col1"] = i;
    dr["Col2"] = i;
    dt.Rows.InsertAt(dr, 0);
    i++;
}



我试图使用dataGrid1.ScrollIntoView(dataGrid1.SelectedItem);但它仍然需要选择项目,甚至它向下滚动到最后一行视图



我可以设法在Windows格式的DataGridView中以下列方式执行相同操作/>


i tried to use dataGrid1.ScrollIntoView(dataGrid1.SelectedItem); but it still need selected item, and even it scroll down till last row in view

I can manage to do the same in DataGridView of windows form in following way

DataGridViewCell cell = dataGridView1.FirstDisplayedCell;
dt.Rows.InsertAt(dr, 0);
dataGridView1.FirstDisplayedCell = cell;



寻找在WPF中执行此操作的任何类似方法



谢谢。


looking for any similar ways to do this in WPF also

Thanks.

推荐答案





那么是什么阻止你插入对所选项目进行排序和/或使用创建的行来调用 ScrollIntoView



所以它应该足够了写:

Hi,

so what''s preventing you from making the inserted row either the selected item and/or using the created row to call ScrollIntoView?

So it should be enough to write:
void dt_Tick(object sender, EventArgs e)
{
   DataRow dr;
   dr = dt.NewRow();
   dr["Col1"] = i;
   dr["Col2"] = i; 
   dt.Rows.InsertAt(dr, 0);
   i++;

   // either do a scroll to inserted row
   dataGrid1.ScrollIntoView(dr);

   // or select item
   // dataGrid1.SelectedItem = dr;
   

   // and then scroll selected item into view
   // dataGrid1.ScrollIntoView(dataGrid1.SelectedItem);
}





希望这会有所帮助。



Hope this helps.


这篇关于WPF C#datagrid修复视图行(修复滚动)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆