在 Silverlight ViewModel 上实现属性

发布于 2024-07-28 23:04:07 字数 911 浏览 2 评论 0原文

我有一个 Silverlight ModelViewViewModel 项目,我想将视图模型上的属性公开给 UserControl,例如:

public DTO.Client Client
{
    get { return client; }
}

client 是我在异步完成事件处理程序中设置的私有支持变量:

    void GetClientByIDComplete(object sender, GetClientByIDCompletedEventArgs e)
    {
        Application.Current.RootVisual.Dispatcher.BeginInvoke(() =>
        {
            DTO.Client c = new ServiceContract.DTO.Client();
            c = e.Result as DTO.Client;
            client = e.Result as DTO.Client;
        });
    }

在我的 Silverlight 页面中,我有以下内容:

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

数据永远不会显示。 如果我将属性更改为: 将显示数据:

public DTO.Client Client
{
    get { 
          client.Name = "My Name";
          return client; }
}

这样我就显式地分配了值。

我需要更改什么才能让我的页面看到该属性?

I have a Silverlight ModelViewViewModel project that I would like to expose a property on the view model to a UserControl like:

public DTO.Client Client
{
    get { return client; }
}

client is a private backing variable that I set in a async completed event handler:

    void GetClientByIDComplete(object sender, GetClientByIDCompletedEventArgs e)
    {
        Application.Current.RootVisual.Dispatcher.BeginInvoke(() =>
        {
            DTO.Client c = new ServiceContract.DTO.Client();
            c = e.Result as DTO.Client;
            client = e.Result as DTO.Client;
        });
    }

In my Silverlight page I have the following:

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

The data never is displayed. The data is displayed if I change the property to:

public DTO.Client Client
{
    get { 
          client.Name = "My Name";
          return client; }
}

This way I explicitly assign the value.

What do I have to change so the property is seen by my page?

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

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

发布评论

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

评论(1

蹲在坟头点根烟 2024-08-04 23:04:07

您是在填充客户端之后还是之前设置数据上下文?

在它肯定应该显示之后,之前,它不会知道底层数据已更改,而无需添加额外的代码,实现 INotifyPropertyChanged 以便绑定可以了解数据已更改。

Are you setting the data context after Client is populated, or before?

After it should definately display, before, it will not know the underlying data has changed without adding additional code, implement INotifyPropertyChanged so that bindings can understand the data has altered.

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