如何重新加载DataContext?

发布于 2024-09-28 23:38:24 字数 1520 浏览 2 评论 0原文

我有一个 Silverlight 业务应用程序(RIA 服务)并且我有一个附加到数据源的 DataContext 的 DataGrid。在 Silverlight 子 Windows 中,我创建一个新实体并将更改提交到服务器。 问题是我的 DataContext 不知道这一点,因此网格不显示新添加的实体。

如何刷新 DataContext 或告诉 DataGrid 重新绑定?

编辑:这是我的代码

<riaControls:DomainDataSource AutoLoad="True" d:DesignData="{d:DesignInstance my:Team, CreateList=true}" Height="0" LoadedData="teamDomainDataSource_LoadedData" Name="teamDomainDataSource" QueryName="GetTeamsQuery" Width="0">
        <riaControls:DomainDataSource.DomainContext>
            <my:F1DomainContext />
        </riaControls:DomainDataSource.DomainContext>
</riaControls:DomainDataSource>

<sdk:DataGrid AutoGenerateColumns="False" Height="200" ItemsSource="{Binding ElementName=teamDomainDataSource, Path=Data}" Name="teamDataGrid" RowDetailsVisibilityMode="VisibleWhenSelected" DataContext="{Binding}">
                    <sdk:DataGrid.Columns>
                        <sdk:DataGridTextColumn x:Name="idColumn" Binding="{Binding Path=Id, Mode=OneWay}" Header="Id" IsReadOnly="True" Width="SizeToHeader" />
                        <sdk:DataGridTextColumn x:Name="nameColumn" Binding="{Binding Path=Name}" Header="Name" Width="SizeToHeader" />
                    </sdk:DataGrid.Columns>
</sdk:DataGrid>

如您所见,我的 teamDataGrid 的 ItemSource 是上面定义的 teamDomainDataSource

I have a Silverlight Business Aplication (RIA Services) and I have a DataGrid attached to a DataSource's DataContext. In a Silverlight child Windows I create a new Entity and submit the changes to the server.
The problem is that my DataContext does not know that so the grid does not show the newly added entity.

How do I refresh the DataContext or tell the DataGrid to re-bind?

Edit: Here's my code

<riaControls:DomainDataSource AutoLoad="True" d:DesignData="{d:DesignInstance my:Team, CreateList=true}" Height="0" LoadedData="teamDomainDataSource_LoadedData" Name="teamDomainDataSource" QueryName="GetTeamsQuery" Width="0">
        <riaControls:DomainDataSource.DomainContext>
            <my:F1DomainContext />
        </riaControls:DomainDataSource.DomainContext>
</riaControls:DomainDataSource>

<sdk:DataGrid AutoGenerateColumns="False" Height="200" ItemsSource="{Binding ElementName=teamDomainDataSource, Path=Data}" Name="teamDataGrid" RowDetailsVisibilityMode="VisibleWhenSelected" DataContext="{Binding}">
                    <sdk:DataGrid.Columns>
                        <sdk:DataGridTextColumn x:Name="idColumn" Binding="{Binding Path=Id, Mode=OneWay}" Header="Id" IsReadOnly="True" Width="SizeToHeader" />
                        <sdk:DataGridTextColumn x:Name="nameColumn" Binding="{Binding Path=Name}" Header="Name" Width="SizeToHeader" />
                    </sdk:DataGrid.Columns>
</sdk:DataGrid>

As you can see my teamDataGrid 's ItemSource is the teamDomainDataSource defined above

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

笨死的猪 2024-10-05 23:38:25

因此,在遵循@Kyle建议后,我收到以下错误:

当 CanLoad 为 false 时,无法执行加载操作。当 CanLoad 为 false 时,应禁用调用加载操作的控件

因此我在 google 上搜索,发现 这篇文章展示了一种“不太漂亮”的解决方法......但它确实有效。

这就是我修复它的方法...

teamDataGrid.ItemsSource = null;
teamDataGrid.ItemsSource = ((F1DomainContext)teamDomainDataSource.DomainContext).Teams;

所以我所做的是将数据源重新绑定到我的网格...有人知道更漂亮的方法吗?

So after following @Kyle suggestion I got the following error:

A load operation cannot be performed when CanLoad is false. Controls that invoke load operations should be disabled when CanLoad is false

So I googled around and I found this post which showed a "not too pretty" way of working this around... but it does the trick.

So this is how I fixed it...

teamDataGrid.ItemsSource = null;
teamDataGrid.ItemsSource = ((F1DomainContext)teamDomainDataSource.DomainContext).Teams;

So what I did was rebinding the datasource to my grid... does anybody know a prettier way?

梦在深巷 2024-10-05 23:38:24

这是一个老问题,但我有一个类似的问题,发现 CanLoad 是错误的。在使用 Reflector 对此进行调查后,我发现仅当域上下文发生更改或当前正在保存或加载时(这是我的情况)才设置此设置。

从 DomainDataSource 类

private void UpdateCanLoadProperty()
{
    this.CanLoad = !this.IsSubmittingChanges && !this.HasChanges;
    if (this.HasChanges && this.IsLoadingData)
    {
        this.CancelLoadPrivate();
    }
}

在 load 方法中,您可以看到它如何抛出您收到的错误:

public void Load()
{
    if (!DesignerProperties.IsInDesignTool)
    {
        if (this.DomainContext == null)
        {
            throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, DomainDataSourceResources.OperationNeedsPropertySet, new object[] { "DomainContext", DomainDataSourceResources.LoadOperation }));
        }
        if (string.IsNullOrEmpty(this.QueryName))
        {
            throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, DomainDataSourceResources.OperationNeedsPropertySet, new object[] { "QueryName", DomainDataSourceResources.LoadOperation }));
        }
        if (this._loadDeferLevel > 0)
        {
            throw new InvalidOperationException(DomainDataSourceResources.LoadWithinDeferLoad);
        }
        this.ValidateQueryParameters();
        if (this._preparingOperation)
        {
            throw new InvalidOperationException(DomainDataSourceResources.InvalidOperationDuringLoadOrSubmit);
        }
        if (!this.CanLoad)
        {
            throw new InvalidOperationException(DomainDataSourceResources.CannotLoadWhenCanLoadIsFalse);
        }
        this.ExecuteLoad(this.InitialLoadType);
    }
}

要处理此错误,您需要提交更改(或拒绝它们,以两者为准)正确的)。如果您使用 SubmitChanges 方法提交更改,您会发现它包含一个采用回调的重载,您希望在该回调中调用 Load 方法。本质上,当有变化时你不能调用load。

This is an old question, but I had a similar problem and found that CanLoad was false. After investigating this with Reflector, I found that this is set only when the domain context has changes or is currently saving or loading (which was my case).

From the DomainDataSource class

private void UpdateCanLoadProperty()
{
    this.CanLoad = !this.IsSubmittingChanges && !this.HasChanges;
    if (this.HasChanges && this.IsLoadingData)
    {
        this.CancelLoadPrivate();
    }
}

And in the load method, you can see how it throws the error that you're receiving:

public void Load()
{
    if (!DesignerProperties.IsInDesignTool)
    {
        if (this.DomainContext == null)
        {
            throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, DomainDataSourceResources.OperationNeedsPropertySet, new object[] { "DomainContext", DomainDataSourceResources.LoadOperation }));
        }
        if (string.IsNullOrEmpty(this.QueryName))
        {
            throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, DomainDataSourceResources.OperationNeedsPropertySet, new object[] { "QueryName", DomainDataSourceResources.LoadOperation }));
        }
        if (this._loadDeferLevel > 0)
        {
            throw new InvalidOperationException(DomainDataSourceResources.LoadWithinDeferLoad);
        }
        this.ValidateQueryParameters();
        if (this._preparingOperation)
        {
            throw new InvalidOperationException(DomainDataSourceResources.InvalidOperationDuringLoadOrSubmit);
        }
        if (!this.CanLoad)
        {
            throw new InvalidOperationException(DomainDataSourceResources.CannotLoadWhenCanLoadIsFalse);
        }
        this.ExecuteLoad(this.InitialLoadType);
    }
}

To handle this error you'll need to submit your changes (or reject them, whichever is correct). If you're submitting your changes by using the SubmitChanges method you'll find that it contains an overload taking a callback which is where you want to call the Load method. In essence, you can't call load when there are changes.

北座城市 2024-10-05 23:38:24

确保您的数据网格的“列表”类型为:ObservableCollection。易于实施并允许此类通知。

如果您需要进一步的帮助,请告诉我。

Make sure your Datagrid's "List" is of type: ObservableCollection. Easy to implement and allows for such notification.

Let me know if you need further assistance.

谈情不如逗狗 2024-10-05 23:38:24

两个最佳选择是 (1) 使用 DomainDataSource.Load() 重新加载 DDS 以获取最新数据,或 (2) 在使用 DomainDataSource.DataView.Add() 提交之前通过 DDS 添加实体。

Your two best options are to either (1) reload the DDS using DomainDataSource.Load() to get the latest data or (2) add the entity via the DDS before submitting it using DomainDataSource.DataView.Add().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文