C# 在Silverlight 4中插入后无法刷新/更新数据网格

C# 在Silverlight 4中插入后无法刷新/更新数据网格,c#,silverlight,datagrid,silverlight-4.0,C#,Silverlight,Datagrid,Silverlight 4.0,我正在创建master detials silverlight页面,在插入新记录后更新数据网格时遇到问题。在页面顶部,我有文本框输入信息,当用户单击save时,我希望信息在数据库中自动更新并显示在datagrid上,而无需刷新屏幕 我能够毫无问题地将信息保存到数据库中,它只是试图让datagrid使用新的更改进行刷新 下面是我为“保存”按钮编写的一些代码: ViewModel.UpdateWorkflow(summary, reason, Email); LoadOperation<Doc

我正在创建master detials silverlight页面,在插入新记录后更新数据网格时遇到问题。在页面顶部,我有文本框输入信息,当用户单击save时,我希望信息在数据库中自动更新并显示在datagrid上,而无需刷新屏幕

我能够毫无问题地将信息保存到数据库中,它只是试图让datagrid使用新的更改进行刷新

下面是我为“保存”按钮编写的一些代码:

ViewModel.UpdateWorkflow(summary, reason, Email);
LoadOperation<Document> lo = _Context.Load<Document>(_Context.GetDocumentsQuery(_DocID), rtRefresh, null);

非常感谢您的帮助。

我假设您的datagrid绑定到IEnumerable,或者列表?您是否尝试过使用ObservableCollection?如果您绑定到ObservableCollection,它将在集合发生更改时通知UI

<UserControl x:Class="SilverlightApplication1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:SilverlightApplication1"
xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">

<UserControl.DataContext>
    <local:MainPage_ViewModel/>
</UserControl.DataContext>

<StackPanel>
    <data:DataGrid ItemsSource="{Binding MyCollection}">

    </data:DataGrid>
</StackPanel>

public类主页面\u视图模型:INotifyPropertyChanged
{
公共可观测集合MyCollection
{
获取{return myCollection;}
设置
{
if(myCollection!=值)
{
myCollection=value;
关于财产变更(“MyCollection”);
}
}
}
私有ObservableCollection myCollection=新ObservableCollection();
公共void LoadData()
{
//执行load方法,将结果分配给MyCollection
}
#区域INotifyProperty更改成员
公共事件属性更改事件处理程序属性更改;
公共void OnPropertyChanged(字符串propertyName)
{
if(PropertyChanged!=null)
{
PropertyChanged(这是新的PropertyChangedEventArgs(propertyName));
}
}
#端区
}

在我的ViewModel中,我最初创建了一个新的DomainContext():

最后,我不得不将以下行复制到我的查询中:

_Context = new appDomainContext();
并粘贴到从数据库获取值的函数:

public void GetDocuments(int dID)
    {           
            _Context = new appDomainContext();  //ADDED THIS LINE TO FIX MY ISSUE                              
            _Context.Load(_Context.GetDocumentsQuery(dID), dlo =>
                {
                    DocumentsView = dlo.Entities;                                                
                }, null);                                
        }
    }

当时的环境是保留旧的价值观。这清除了_上下文并允许我在_上下文中更新数据。

您的问题可能与域上下文Load()方法的默认加载行为有关。尝试将LoadBehavior显式设置为LoadBehavior.RefreshCurrent。有关LoadBehavior的更多信息,请参阅:

有时,例如,在删除之后,您还需要清除()域上下文中的EntityContainer,或者至少清除()您尝试加载的实体的特定EntitySet,以便显示正确的实体


比如MyDomainContext.EntityContainer.GetEntitySet().Clear()

是否只是将GetDocumentsQuery()从IEnumerable更改为ObservableCollection的问题??或者它比这更复杂?我需要看到您的XAML和viewModel才能回答这个问题。datagrid应该有一个ItemsSource,它绑定到viewModel上的ObservableCollection。从GetDocumentsQuery()获取数据时,应将其分配给viewModel上的ObservableCollection。这有意义吗?谢谢你的帮助。我最终解决了我的问题。我最终没有使用ObservableCollection,但你让我明白了为什么我不能让它工作。在我的VieModel中,我在第一次加载时创建了一个_Context=new DomainContext(),出于某种原因,每次我尝试更新数据网格时,旧值都保持不变。因此,我最终将_Context=new DomainContext()放在了实际刷新datagrid的函数中,它成功了!
<UserControl x:Class="SilverlightApplication1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:SilverlightApplication1"
xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">

<UserControl.DataContext>
    <local:MainPage_ViewModel/>
</UserControl.DataContext>

<StackPanel>
    <data:DataGrid ItemsSource="{Binding MyCollection}">

    </data:DataGrid>
</StackPanel>
public class MainPage_ViewModel : INotifyPropertyChanged
{
    public ObservableCollection<object> MyCollection
    {
        get { return myCollection; }
        set 
        {
            if (myCollection != value)
            {
                myCollection = value;
                OnPropertyChanged("MyCollection");
            }
        }
    }
    private ObservableCollection<object> myCollection = new ObservableCollection<object>();

    public void LoadData()
    {
        //execute load method, the assign result to MyCollection
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    #endregion
}
public DocumentWindowViewModel()
    {
        _Context = new appDomainContext();                       
        WireCommands();
    }
_Context = new appDomainContext();
public void GetDocuments(int dID)
    {           
            _Context = new appDomainContext();  //ADDED THIS LINE TO FIX MY ISSUE                              
            _Context.Load(_Context.GetDocumentsQuery(dID), dlo =>
                {
                    DocumentsView = dlo.Entities;                                                
                }, null);                                
        }
    }