My application's main component is a tab control which holds N number of views and those views' datacontext is a separate ViewModel object. I have a statusbar at the bottom of the app and it contains a few textboxes. I want one of the textboxes to reflect a timestamp for the currently selected tab. The timestamp is a property of the ViewModel object that's set as the view's datacontext.
开发者_如何学编程I'm a WPF newb and not really sure how to bind that property to the status bar.
Make sure your ViewModel implements INotifyPropertyChanged.
For example...
/// <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
}
Something like:
<TextBox Text="{Binding ElementName=tabControl, Path=SelectedItem.DataContext.Timestamp}" />
A little depending on if your tabcontrol's itemssource is databound or not.
精彩评论