WPF绑定不更新视图

szp*_*pic 18 c# wpf xaml mvvm

我有一个文本块:

<TextBlock HorizontalAlignment="Left" Name="StatusText" Margin="0,20" TextWrapping="Wrap" Text="{Binding StatusText}">
            ... Status ...
</TextBlock>
Run Code Online (Sandbox Code Playgroud)

代码隐藏:

public StatusPage()
{
    InitializeComponent();
    this.DataContext = new StatusPageViewModel(this);
}
Run Code Online (Sandbox Code Playgroud)

并在viewModel中:

private string _statusText;
/// <summary>
/// Status text
/// </summary>
public string StatusText
{
    get { return _statusText; }
    set { _statusText = value; }
}
Run Code Online (Sandbox Code Playgroud)

并在viewModel中的函数:

string statusText = Status.GetStatusText();
this.StatusText = statusText;
Run Code Online (Sandbox Code Playgroud)

GetStatusText()返回字符串,如"完成工作"等.来自该函数的值被赋予this.StatusText但TextBlock的文本属性不会更改并显示仍然占位符"...状态..."

我知道这样的问题 - > 点击 <---但是看完之后我仍然无法找到解决方案

@Update

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

public string StatusText
{
    get 
    {
        return _statusText;
    }
    set 
    {
        _statusText = value; 
        RaisePropertyChanged("StatusText");
    }
}
Run Code Online (Sandbox Code Playgroud)

和viewModel的声明:

 public class StatusPageViewModel : ObservableObject, INavigable
Run Code Online (Sandbox Code Playgroud)

哪里:

ObservableObject类是:

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
}
Run Code Online (Sandbox Code Playgroud)

但它仍然无法正常工作

toa*_*akz 25

您需要INotifyPropertyChanged在ViewModel命令中实现,以通知View属性已更改.

这是MSDN页面的链接:System.ComponentModel.INotifyPropertyChanged

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

  • 尝试将“Binding.Mode”属性设置为“OneWay” - 可能是 UI 未侦听更新。 (2认同)

May*_*gra 10

两种方式添加绑定模式,因为默认情况下Textblock的绑定模式是一种方式

<TextBlock HorizontalAlignment="Left" Name="StatusText" Margin="0,20" TextWrapping="Wrap" Text="{Binding StatusText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
            ... Status ...
</TextBlock>
Run Code Online (Sandbox Code Playgroud)

当然,您需要为此目的实现INotifyPropertyChanged,请参阅此链接以了解如何实现.

  • 使绑定twoway没有意义,因为控件无法更改文本,因此无需更新源. (10认同)

Her*_*lom 6

使用数据模型时,您必须确保模型在初始加载时完整。因此,如果您这样做: this.DataContext = mainViewModel 并且 mainViewModel 的某些部分未加载(=null),那么您将无法绑定它们。例如,我有一个模型,该模型中有一个对象程序。我将 TextBlock 的文本绑定到 Model.Program.Name。程序对象在初始加载时未连接,因此您必须在之后重新绑定到加载的对象,否则无法发送通知。