简体   繁体   English

更新TextBlock绑定

[英]Updating a TextBlock Binding

I'm working on a WP7 app and I've had some trouble updating a TextBlock bound to a property. 我正在开发WP7应用程序,但在更新绑定到属性的TextBlock遇到了一些麻烦。 I'm new to MVVM, and C# in general, so I'm not sure what I'm doing wrong. 我是MVVM和C#的新手,所以我不确定自己做错了什么。

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). Status是一个enum ,当由我的应用设置时,它也会同时设置StatusMessage (这是更易于理解的描述,以向用户显示)。 My View's TextBlock is bound to StatusMessage , but it doesn't update using the above code. 我的视图的TextBlock绑定到StatusMessage ,但不会使用上述代码进行更新。

However, if I move NotifyPropertyChanged("StatusMessage") into Status , my View's TextBlock updates like it should. 但是,如果我将NotifyPropertyChanged("StatusMessage")移到Status ,则我的View的TextBlock像应该的那样进行更新。 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 :) 在此先感谢您帮助新手:)

Issue in this line: 此行中的问题:

 statusMessage = ConvertStatusToSomethingMeaningful(value);

StatusMessage setter is never called (NotifyPropertyChanged("StatusMessage") called exactly there) 从不调用StatusMessage设置程序(确切地在其中调用NotifyPropertyChanged(“ StatusMessage”))

 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); }
           }

      ...
      }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM