WPF-查看模型属性的数据绑定窗口标题

WPF-查看模型属性的数据绑定窗口标题,wpf,data-binding,window,Wpf,Data Binding,Window,我正在尝试将窗口标题绑定到视图模型中的属性,如下所示: Title="{Binding WindowTitle}" 该属性如下所示: /// <summary> /// The window title (based on profile name) /// </summary> public string WindowTitle { get { return CurrentProfileName + " - Ba

我正在尝试将窗口标题绑定到视图模型中的属性,如下所示:

Title="{Binding WindowTitle}"
该属性如下所示:

    /// <summary>
    /// The window title (based on profile name)
    /// </summary>
    public string WindowTitle
    {
        get { return CurrentProfileName + " - Backup"; }
    }
//
///窗口标题(基于配置文件名称)
/// 
公共字符串窗口标题
{
获取{return CurrentProfileName+“-Backup”;}
}
CurrentProfileName属性是从另一个属性(CurrentProfilePath)派生的,该属性在有人打开或保存配置文件时设置。在初始启动时,窗口标题设置正确,但当CurrentProfilePath属性发生更改时,更改不会像我预期的那样冒泡到窗口标题

我认为我不能在这里使用依赖属性,因为该属性是派生属性。派生它的基本属性是依赖属性,但这似乎没有任何效果


如何基于此属性使表单标题自我更新?

这是因为WPF无法知道
WindowTitle
依赖于
CurrentProfileName
。您的类需要实现
INotifyPropertyChanged
,当您更改
CurrentProfileName
的值时,需要为
CurrentProfileName
WindowTitle
引发
PropertyChanged
事件

private string _currentProfileName;
public string CurrentProfileName
{
    get { return __currentProfileName; }
    set
    {
        _currentProfileName = value;
        OnPropertyChanged("CurrentProfileName");
        OnPropertyChanged("WindowTitle");
    }
}

更新

以下是INotifyPropertyChanged的典型实现:

public class MyClass : INotifyPropertyChanged
{
    // The event declared in the interface
    public event PropertyChangedEventHandler PropertyChanged;

    // Helper method to raise the event
    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName);
    }

    ...
}

答对 了成功了。第二个问题-现有OnPropertyChanged方法仅接受DependencyProperty对象-是否确实需要将实际的PropertyChanged事件和自定义OnPropertyChanged方法添加到我的类中,还是有更简单的方法?是的,您需要声明该事件。现有的
OnPropertyChanged
方法是从
DependencyObject
继承而来的,它只用于依赖属性。是的,我最终做的正是您的第二个示例所显示的。我只是想知道我是不是走了很长的路。谢谢