Silverlight 从 RIA 服务绑定到 TextBlock

发布于 2024-08-03 07:04:05 字数 701 浏览 0 评论 0原文

我有一个看起来像这样的 TextBlock:

<TextBlock Text="{Binding Name}" />

它位于 内,DataContext 设置为 ViewModel 中的 MyClient:

public Client MyClient { get; private set; } // This is a RIA Entity, hence supports INotifyPropertyChanged

public ViewModel() {
    MyClient = new Client();
    LoadOperation<Client> loadClient = RiaContext.Load<Client>(RiaContext.GetClientsQuery());
    loadClient.Completed += new EventHandler(loadClient_Completed);
}

void loadClient_Completed(object sender, EventArgs e) {
    MyClient = DB.Clients.Single();
}

像上面那样设置 MyClient 不会引发 PropertyChanged 事件。因此,用户界面永远不会更新。

I've a TextBlock that looks like so:

<TextBlock Text="{Binding Name}" />

This is inside a <Canvas> with the DataContext set to MyClient which is in the ViewModel:

public Client MyClient { get; private set; } // This is a RIA Entity, hence supports INotifyPropertyChanged

public ViewModel() {
    MyClient = new Client();
    LoadOperation<Client> loadClient = RiaContext.Load<Client>(RiaContext.GetClientsQuery());
    loadClient.Completed += new EventHandler(loadClient_Completed);
}

void loadClient_Completed(object sender, EventArgs e) {
    MyClient = DB.Clients.Single();
}

Setting MyClient like the above does not raise the PropertyChanged event. As such the UI is never updated.

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

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

发布评论

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

评论(3

寂寞美少年 2024-08-10 07:04:05

这就是我最终所做的。我添加了一个在 RIA 回调完成时触发的事件。然后,我在视图中附加一个处理程序,将 DataContext 设置为 ViewModel。因此,它会有效地等待 ViewModel 获取数据,然后将 DataContext 设置为 ViewModel - 从而获得正确的数据。

Here's what I ended up doing. I added an event which is triggered when the RIA callback is complete. I then attach a handler to this in the view which sets the DataContext to the ViewModel. So effectively, it waits until the ViewModel has grabbed the data, and then it sets the DataContext to the ViewModel - thus getting the correct data.

梦晓ヶ微光ヅ倾城 2024-08-10 07:04:05

您应该设置 OneWay 或 TwoWay 绑定。

<TextBlock Text="{Binding Name, Mode=OneWay}" />
<TextBlock Text="{Binding Name, Mode=TwoWay}" />

默认情况下,我相信绑定是一次性的。

You should set OneWay or TwoWay binding.

<TextBlock Text="{Binding Name, Mode=OneWay}" />
<TextBlock Text="{Binding Name, Mode=TwoWay}" />

By default I believe binding does OneTime.

゛清羽墨安 2024-08-10 07:04:05

UI 永远不会更新,因为您正在替换 UI 附加到的对象。
替换发生在 loadClient_completed 方法上。

The UI never updates because you are replacing the object the UI is attached to.
The replacement happens on the loadClient_completed method.

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