WPF绑定更新不及时的看法

WPF绑定更新不及时的看法

问题描述:

我有一个文本块:

<TextBlock HorizontalAlignment="Left" Name="StatusText" Margin="0,20" TextWrapping="Wrap" Text="{Binding StatusText}">
            ... Status ...
</TextBlock>



代码隐藏:

codebehind:

public StatusPage()
{
    InitializeComponent();
    this.DataContext = new StatusPageViewModel(this);
}

和在视图模型:

private string _statusText;
/// <summary>
/// Status text
/// </summary>
public string StatusText
{
    get { return _statusText; }
    set { _statusText = value; }
}

和在视图模型功能:

string statusText = Status.GetStatusText();
this.StatusText = statusText;



GetStatusText()返回字符串,如工作做...状态等,从功能assinged到 this.StatusText 但TextBlock中的文本属性不改变,并呈现出仍然占位符值......

GetStatusText() returns string like "Work done" etc. Values from that functions are assinged to the this.StatusText but the TextBlock's text property don't change and is showing still placeholder "... Status..."

我所知道的这样的问题 - >
点击&LT; ---但看完这个我还是没能找到解决办法。

I'm aware of questions like this --> CLICK<--- but after reading this I'm still not able to find solution

@Update

您的建议后,我更新了我的代码,现在我有这样的:

After your suggestions i updated my code and now I have this:

public string StatusText
{
    get 
    {
        return _statusText;
    }
    set 
    {
        _statusText = value; 
        RaisePropertyChanged("StatusText");
    }
}

和声明视图模型的:

 public class StatusPageViewModel : ObservableObject, INavigable

其中:

ObservableObject类是:

ObservableObject class is:

public abstract class ObservableObject : INotifyPropertyChanged
{
    #region INotifyPropertyChanged Members

    /// <summary>
    /// Raises the PropertyChange event for the property specified
    /// </summary>
    /// <param name="propertyName">Property name to update. Is case-sensitive.</param>
    public virtual void RaisePropertyChanged(string propertyName)
    {
        OnPropertyChanged(propertyName);
    }

    /// <summary>
    /// Raised when a property on this object has a new value.
    /// </summary>
    public event PropertyChangedEventHandler PropertyChanged;

    /// <summary>
    /// Raises this object's PropertyChanged event.
    /// </summary>
    /// <param name="propertyName">The property that has a new value.</param>
    protected virtual void OnPropertyChanged(string propertyName)
    {

        PropertyChangedEventHandler handler = this.PropertyChanged;
        if (handler != null)
        {
            var e = new PropertyChangedEventArgs(propertyName);
            handler(this, e);
        }
    }

    #endregion // INotifyPropertyChanged Members
}

但它仍然没有工作。

您需要执行 INotifyPropertyChanged的在您的视图模型以通知认为,属性已更改

You need to implement INotifyPropertyChanged in your ViewModel order to notify the View that the property has changed.

下面是到MSDN页面的链接吧:的System.ComponentModel.INotifyPropertyChanged

Here's a link to the MSDN page for it: System.ComponentModel.INotifyPropertyChanged

要注意的最重要的事情是,你应该提高你的属性setter的的PropertyChanged 事件。

The most important thing to note is that you should raise the PropertyChanged event in your property setter.