【发布时间】:2014-10-20 17:55:20
【问题描述】:
我有一个文本块:
<TextBlock HorizontalAlignment="Left" Name="StatusText" Margin="0,20" TextWrapping="Wrap" Text="{Binding StatusText}">
... Status ...
</TextBlock>
代码隐藏:
public StatusPage()
{
InitializeComponent();
this.DataContext = new StatusPageViewModel(this);
}
在 viewModel 中:
private string _statusText;
/// <summary>
/// Status text
/// </summary>
public string StatusText
{
get { return _statusText; }
set { _statusText = value; }
}
在 viewModel 中的函数中:
string statusText = Status.GetStatusText();
this.StatusText = statusText;
GetStatusText() 返回诸如“工作完成”之类的字符串。来自该函数的值被分配给this.StatusText,但 TextBlock 的文本属性没有改变,并且仍然显示占位符“...状态...”
我知道这样的问题 --> CLICK
@更新
根据您的建议,我更新了我的代码,现在我有了这个:
public string StatusText
{
get
{
return _statusText;
}
set
{
_statusText = value;
RaisePropertyChanged("StatusText");
}
}
和viewModel的声明:
public class StatusPageViewModel : ObservableObject, INavigable
地点:
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
}
但是还是不行
【问题讨论】:
-
你的问题解决了吗?
-
那么解决方案是什么?我面临同样的问题。实现了INotifyPropertyChanged,Mode 1way/2ways没有区别。
-
我也有同样的问题。在应用程序运行时更改绑定属性后(例如 OneWay/TwoWay),属性的 getter 被调用,一切看起来都很好,但重新启动后它仍然无法正常工作。
-
它可能会在将来对某人有所帮助,但是尽管实现了接口,但我忘记在我的 MainWindow 类声明中实际引用它。例如:
MainWindow : Window, INotifyPropertyChanged