WPF 和 ViewModel 属性访问

发布于 2024-10-03 14:33:08 字数 192 浏览 1 评论 0原文

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

糖粟与秋泊 2024-10-10 14:33:09

类似的东西:

<TextBox Text="{Binding ElementName=tabControl, Path=SelectedItem.DataContext.Timestamp}" />

有点取决于您的选项卡控件的项目源是否是数据绑定的。

Something like:

<TextBox Text="{Binding ElementName=tabControl, Path=SelectedItem.DataContext.Timestamp}" />

A little depending on if your tabcontrol's itemssource is databound or not.

月下伊人醉 2024-10-10 14:33:08

确保您的 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
}

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文