WPF 绑定不更新视图 [英] WPF binding not updating the view

查看:39
本文介绍了WPF 绑定不更新视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本块:

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

代码隐藏:

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

在视图模型中:

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

在 viewModel 中的函数:

and in function in viewModel:

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

GetStatusText() 返回类似Work done"等的字符串.来自该函数的值被分配给 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..."

我知道这样的问题-->点击<---但读完这篇我还是找不到解决办法

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

@更新

根据您的建议,我更新了我的代码,现在我有了:

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

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

和viewModel的声明:

and declaration of viewModel:

 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
}

但是还是不行

推荐答案

您需要在您的 ViewModel 订单中实现 INotifyPropertyChanged 以通知 View 属性已更改.

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

需要注意的最重要的事情是您应该在属性设置器中引发 PropertyChanged 事件.

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

这篇关于WPF 绑定不更新视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆