WPF - data binding window title to view model property

后端 未结 1 1355
天涯浪人
天涯浪人 2021-01-13 05:36

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:<

相关标签:
1条回答
  • 2021-01-13 05:45

    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);
        }
    
        ...
    }
    
    0 讨论(0)
提交回复
热议问题