更新 TextBlock 绑定

发布于 2024-12-19 10:54:02 字数 1847 浏览 4 评论 0原文

我正在开发 WP7 应用程序,但在更新绑定到属性的 TextBlock 时遇到了一些问题。我是 MVVM 和 C# 的新手,所以我不确定我做错了什么。

最后我解决了这个问题,但我不明白为什么我的解决方案有效(总是很有趣......),所以我非常感谢您的指导。

在我的应用程序的模型中,我最初有这样的内容:

// Broken
namespace MyApp.MyModel
{
    public class MetaData : INotifyPropertyChanged
    {
        private StatusType status;
        public StatusType Status
        {
            get { return status; }
            set
            {
                status = value;
                statusMessage = ConvertStatusToSomethingMeaningful(value);
            }
        }

        private string statusMessage;
        public string StatusMessage
        {
            get { return statusMessage; }
            private set
            {
                statusMessage = value;
                // This doesn't work
                NotifyPropertyChanged("StatusMessage");
            }
        }

        ...
    }
}

Status是一个enum,当我的应用程序设置它时,它也会设置StatusMessage也是(这是向用户展示的更易读的描述)。我的视图的 TextBlock 绑定到 StatusMessage,但它不会使用上述代码进行更新。

但是,如果我将 NotifyPropertyChanged("StatusMessage") 移动到 Status 中,我的视图的 TextBlock 会按预期更新。但是,我不明白为什么上面的原始代码不起作用时这会起作用?

// Fixed
namespace MyApp.MyModel
{
    public class MetaData : INotifyPropertyChanged
    {
        private StatusType status;
        public StatusType Status
        {
            get { return status; }
            set
            {
                status = value;
                StatusMessage = ConvertStatusToSomethingMeaningful(value);
                // This works
                NotifyPropertyChanged("StatusMessage");
            }
        }

        public string StatusMessage { get; private set; }

        ...
    }
}

非常感谢您帮助新手:)

I'm working on a WP7 app and I've had some trouble updating a TextBlock bound to a property. I'm new to MVVM, and C# in general, so I'm not sure what I'm doing wrong.

In the end I have solved this problem, but I don't understand why my solution works (always fun ...), so I'd really appreciate your guidance.

In my app's Model, I originally had something like this:

// Broken
namespace MyApp.MyModel
{
    public class MetaData : INotifyPropertyChanged
    {
        private StatusType status;
        public StatusType Status
        {
            get { return status; }
            set
            {
                status = value;
                statusMessage = ConvertStatusToSomethingMeaningful(value);
            }
        }

        private string statusMessage;
        public string StatusMessage
        {
            get { return statusMessage; }
            private set
            {
                statusMessage = value;
                // This doesn't work
                NotifyPropertyChanged("StatusMessage");
            }
        }

        ...
    }
}

Status is a enum, and when it's set by my app, it also sets StatusMessage too (which is a more human readable description to show the user). My View's TextBlock is bound to StatusMessage, but it doesn't update using the above code.

However, if I move NotifyPropertyChanged("StatusMessage") into Status, my View's TextBlock updates like it should. However, I don't understand why this works when the original code above doesn't?

// Fixed
namespace MyApp.MyModel
{
    public class MetaData : INotifyPropertyChanged
    {
        private StatusType status;
        public StatusType Status
        {
            get { return status; }
            set
            {
                status = value;
                StatusMessage = ConvertStatusToSomethingMeaningful(value);
                // This works
                NotifyPropertyChanged("StatusMessage");
            }
        }

        public string StatusMessage { get; private set; }

        ...
    }
}

Many thanks in advance for helping a newbie out :)

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

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

发布评论

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

评论(1

把梦留给海 2024-12-26 10:54:02

这一行中的问题:

 statusMessage = ConvertStatusToSomethingMeaningful(value);

StatusMessage setter 永远不会被调用(NotifyPropertyChanged("StatusMessage") 在那里被调用)

 StatusMessage = ConvertStatusToSomethingMeaningful(value);

将是正确的调用

可能我的实现是下一个:

 namespace MyApp.MyModel
 {
      public class MetaData : INotifyPropertyChanged
      {
           private StatusType status;
           public StatusType Status
           {
                get { return status; }
                set
                {
                     if (status != value)
                     {
                          status = value;
                          NotifyPropertyChanged("Status");
                          NotifyPropertyChanged("StatusMessage");
                     }                
                }
           }

           public string StatusMessage
           {
                get { return ConvertStatusToSomethingMeaningful(status); }
           }

      ...
      }
 }

Issue in this line:

 statusMessage = ConvertStatusToSomethingMeaningful(value);

StatusMessage setter is never called (NotifyPropertyChanged("StatusMessage") called exactly there)

 StatusMessage = ConvertStatusToSomethingMeaningful(value);

will be the right call

Probably my implementation of this would be next:

 namespace MyApp.MyModel
 {
      public class MetaData : INotifyPropertyChanged
      {
           private StatusType status;
           public StatusType Status
           {
                get { return status; }
                set
                {
                     if (status != value)
                     {
                          status = value;
                          NotifyPropertyChanged("Status");
                          NotifyPropertyChanged("StatusMessage");
                     }                
                }
           }

           public string StatusMessage
           {
                get { return ConvertStatusToSomethingMeaningful(status); }
           }

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