gpt4 book ai didi

WPF - 用于查看模型属性的数据绑定(bind)窗口标题

转载 作者:行者123 更新时间:2023-12-02 11:11:48 27 4
gpt4 key购买 nike

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

Title="{Binding WindowTitle}"

该属性如下所示:

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

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

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

如何根据此属性使表单标题自动更新?

最佳答案

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

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);
}

...
}

关于WPF - 用于查看模型属性的数据绑定(bind)窗口标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1511053/

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