代码之家  ›  专栏  ›  技术社区  ›  ChadT

Silverlight从RIA服务绑定到TextBlock

  •  0
  • ChadT  · 技术社区  · 16 年前

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

    <Canvas> 将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事件。因此,用户界面永远不会更新。

    3 回复  |  直到 16 年前
        1
  •  0
  •   ChadT    16 年前

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

        2
  •  0
  •   Tacoman667    16 年前

    您应该设置单向绑定或双向绑定。

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

    默认情况下,我相信绑定只需要一次。

        3
  •  0
  •   Klinger    16 年前

    替换发生在loadClient_completed方法上。

    推荐文章