Silverlight 从 RIA 服务绑定到 TextBlock
我有一个看起来像这样的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这就是我最终所做的。我添加了一个在 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.
您应该设置 OneWay 或 TwoWay 绑定。
默认情况下,我相信绑定是一次性的。
You should set OneWay or TwoWay binding.
By default I believe binding does OneTime.
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.