-1

Here is an attempt to run the answers suggested for How do I programmatically change the Title in a wpf window?

<Window x:Class="WindowTitle.MainWindow"
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
     xmlns:local="clr-namespace:WindowTitle"
     mc:Ignorable="d"
     MinWidth="600" SizeToContent="WidthAndHeight" Title="{Binding MyTitle}">
    <Grid Margin="5">
         <Button Click="Button_Click" Content="Change Title" Width="100"/>
    </Grid>
</Window>

Code-behind:

using System.Windows;

namespace WindowTitle
{
    public partial class MainWindow : Window
    {
        private int counter = 0;
        public void Button_Click(object sender, RoutedEventArgs e)
        {
            MyTitle = counter.ToString();
            ++counter;
            System.Diagnostics.Debug.Assert(MyTitle != DefaultTitle);
        }
        public const string DefaultTitle = "Default Title";
        public MainWindow() { InitializeComponent(); DataContext = this; }
        public string MyTitle { get; set; } = DefaultTitle;
    }
}

The code sets the title on load but not on subsequent click of the button.

Any ideas? I'm stumped.

ingvar
  • 4,169
  • 4
  • 16
  • 29
We B Martians
  • 367
  • 2
  • 12

2 Answers2

0

The binding doesn't know that MyTitle has changed. The common way of communicating the change is to implement INotifyPropertyChanged; also take a look at MVVM, which is a very common way implement it via DataContext.

Curt Nichols
  • 2,757
  • 1
  • 17
  • 24
0

I thought that INotifyPropertyChanged was automatically supported; I'm wrong. Here's is the working code:

<Window x:Class="WindowTitle.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:WindowTitle"
            mc:Ignorable="d"
            MinWidth="400"
            SizeToContent="WidthAndHeight"
            Title="{Binding MainWindowTitle}">
    <Grid Margin="5">
        <Button Click="Button_Click"
                    Content="Change by Binding"
                    HorizontalAlignment="Left"
                    Padding="5,0"/>
        <Button x:Name="ChangeBtn"
                    Click="ChangeBtn_Click"
                    Content="Change by Name"
                    HorizontalAlignment="Right"
                    Padding="5,0"/>
    </Grid>
</Window>
using System.Windows;
namespace WindowTitle {
    public partial class MainWindow : Window, System.ComponentModel.INotifyPropertyChanged {
        public void Button_Click(object sender, RoutedEventArgs e) {
            MainWindowTitle = titleCount.ToString();
            ++titleCount;
        }
        void ChangeBtn_Click(object sender, RoutedEventArgs e) {
            --titleCount;
            Title = titleCount.ToString();
        }
        public MainWindow() { InitializeComponent(); DataContext = this; }
        public string MainWindowTitle {
            get { return mainWindowTitleBase; }
            set {
                value = value.Trim();
                if (value != mainWindowTitleBase) {
                    mainWindowTitleBase = value;
                    OnPropertyChanged("MainWindowTitle");
                }
            }
        }
        string mainWindowTitleBase = "Default Title";
        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
        int titleCount = 0;
        protected void OnPropertyChanged(string /*property name*/ ptyNam) {
            PropertyChanged?.Invoke(this, new System.ComponentModel.PropertyChangedEventArgs(ptyNam));
        }
    }
}
We B Martians
  • 367
  • 2
  • 12