WPF - 数据绑定窗口标题查看模型属性 [英] WPF - data binding window title to view model property

查看:399
本文介绍了WPF - 数据绑定窗口标题查看模型属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  Title = {Binding WindowTitle}

该属性如下所示:

  ///< summary> 
///窗口标题(基于配置文件名称)
///< / summary>
public string WindowTitle
{
get {return CurrentProfileName + - Backup;
}

CurrentProfileName属性派生自另一个属性(CurrentProfilePath)有人打开或保存个人资料。在初始启动时,窗口标题设置正确,但当CurrentProfilePath属性更改时,更改不会像我预期的那样冒泡到窗口标题。



我不认为我可以使用依赖属性,因为该属性是派生的属性。派生的基础属性是依赖属性,但似乎没有任何影响。



如何使表单标题自动更新在这个属性上?

解决方案

这是因为WPF无法知道 WindowTitle 取决于 CurrentProfileName 。您的课程需要实现 INotifyPropertyChanged ,当您更改 CurrentProfileName 的值时,您需要提高 PropertyChanged 事件 CurrentProfileName WindowTitle / p>

 私人字符串_currentProfileName; 
public string CurrentProfileName
{
get {return __currentProfileName; }
set
{
_currentProfileName = value;
OnPropertyChanged(CurrentProfileName);
OnPropertyChanged(WindowTitle);
}
}






更新



以下是 INotifyPropertyChanged 的典型实现:

  public class MyClass:INotifyPropertyChanged 
{
//在界面中声明的事件
public event PropertyChangedEventHandler PropertyChanged;

//辅助方法来引发事件
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if(handler!= null)
处理程序(这个,新的PropertyChangedEventArgs(propertyName);
}

...
}


I'm trying to bind my window title to a property in my view model, like so:

Title="{Binding WindowTitle}"

The property looks like this:

    /// <summary>
    /// The window title (based on profile name)
    /// </summary>
    public string WindowTitle
    {
        get { return CurrentProfileName + " - Backup"; }
    }

The CurrentProfileName property is derived from another property (CurrentProfilePath) that is set whenever someone opens or saves profile. On initial startup, the window title is set properly, but when ever the CurrentProfilePath property changes, the change doesn't bubble up to the window title like I expected it would.

I don't think I can use a dependency property here because the property is a derived one. The base property from which it is derived is a dependency property, but that doesn't seem to have any effect.

How can I make the form title self-updating based on this property?

解决方案

That's because WPF has no way of knowing that WindowTitle depends on CurrentProfileName. Your class needs to implement INotifyPropertyChanged, and when you change the value of CurrentProfileName, you need to raise the PropertyChanged event for CurrentProfileName and WindowTitle

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


UPDATE

Here's a typical implementation of 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);
    }

    ...
}

这篇关于WPF - 数据绑定窗口标题查看模型属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆