如何为主细节datagridview设置自动刷新绑定到实体框架? [英] How to set auto-refresh for master detail datagridview bound to entity framework?

查看:53
本文介绍了如何为主细节datagridview设置自动刷新绑定到实体框架?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个WPF Master详细信息应用程序。我想每隔5秒自动刷新一次页面。我使用Dispatcher来实现这一目标。一切正常,但所选项目仅在5秒钟内突出显示,5秒后,默认选定行显示在顶部,替换我的选定项目。



I尝试设置issynchronizedwithcurrentitem =False,但是如果设置为false,则selecteditem(对于Master-detail场景)不起作用。



这是我试过的,



I am developing a WPF Master detail application. I would like to auto refresh my page every 5 secs. I have used a Dispatcher to achieve this. everything works fine, but the selected item is highlighted only for those 5 seconds, after 5 secs the default selected row is being displayed on the top replacing my selecteditem.

I have tried setting the issynchronizedwithcurrentitem="False", but with this set to false, the selecteditem ( for Master-detail scenario) doesn''t work.

this is what I have tried,

public partial class MainWindow : Window
{
    WpfApplication1.PersonsEntities personsEntities;
    System.Windows.Data.CollectionViewSource personViewSource;
    System.Data.Objects.ObjectQuery<WpfApplication1.person> personQuery;

    public MainWindow()
    {
        InitializeComponent();
    }

    private ObjectQuery<person> GetpersonQuery(PersonsEntities personsEntities)
    {
        var personQuery = personsEntities.person;
        return personQuery as ObjectQuery<person>;
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        DispatcherTimer dispatcherTimer = new DispatcherTimer();
        dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
        dispatcherTimer.Interval = new TimeSpan(0, 0, 5);
        dispatcherTimer.Start();
    }

    protected void dispatcherTimer_Tick(object sender, EventArgs e)
    {
        personsEntities = new WpfApplication1.PersonsEntities();
        personViewSource = ((System.Windows.Data.CollectionViewSource)  (this.FindResource("personViewSource")));
        personQuery = this.GetpersonQuery(personsEntities);
        personViewSource.Source = personQuery.Execute(System.Data.Objects.MergeOption.AppendOnly);
    }
}

推荐答案

我认为它可以这样工作:获取 personViewSource.View.CurrentItem 在分配新的 Source 之前。之后使用 personViewSource.View.MoveCurrentTo 再次选择它。再说一遍:我认为这应该有用,但我没有时间设置一个例子并试一试 - 抱歉。
I think it can work like this: Get your personViewSource.View.CurrentItem before assigning the new Source. After that use personViewSource.View.MoveCurrentTo to select it again. Again: I "think" this should work, but I had no time to set up an example and try it - sorry.


这篇关于如何为主细节datagridview设置自动刷新绑定到实体框架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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