gpt4 book ai didi

WPF 和 ViewModel 属性访问

转载 作者:行者123 更新时间:2023-12-03 10:22:01 26 4
gpt4 key购买 nike

我的应用程序的主要组件是一个选项卡控件,它包含 N 个 View ,这些 View 的数据上下文是一个单独的 ViewModel 对象。我在应用程序底部有一个状态栏,它包含一些文本框。我希望其中一个文本框反射(reflect)当前选定选项卡的时间戳。时间戳是 ViewModel 对象的一个​​属性,它被设置为 View 的数据上下文。

我是 WPF 新手,不确定如何将该属性绑定(bind)到状态栏。

最佳答案

确保您的 ViewModel 实现了 INotifyPropertyChanged。

例如...

/// <summary>
/// Sample ViewModel.
/// </summary>
public class ViewModel : INotifyPropertyChanged
{
#region Public Properties

/// <summary>
/// Timestamp property
/// </summary>
public DateTime Timestamp
{
get
{
return this._Timestamp;
}
set
{
if (value != this._Timestamp)
{
this._Timestamp = value;

// NOTE: This is where the ProperyChanged event will get raised
// which will result in the UI automatically refreshing itself.
OnPropertyChanged("Timestamp");
}
}
}

#endregion


#region INotifyPropertyChanged Members

/// <summary>
/// Event
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;

/// <summary>
/// Raise the PropertyChanged event.
/// </summary>
protected void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}

#endregion


#region Private Fields

private DateTime _Timestamp;

#endregion
}

关于WPF 和 ViewModel 属性访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4219242/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com