Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF bind title of window to property

I am trying to bind the value of a property (MyTitle) of a class (MainWindow) that derives from Window. I have created a dependency property called MyTitleProperty, implemented the INotifyPropertyChanged interface and modified the set method of MyTitle to call the PropertyChanged event, passing "MyTitle" as the property name parameters. I set MyTitle to "Title" in the constructor but when the window opens the title is blank. If I put a break point on the Loaded event then MyTitle = "Title" but this.Title = "". This is surely something unbelievably obvious that I've not noticed. Please help!

MainWindow.xaml

<Window
    x:Class="WindowTitleBindingTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:this="clr-namespace:WindowTitleBindingTest"
    Height="350"
    Width="525"
    Title="{Binding Path=MyTitle, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type this:MainWindow}}}"
    Loaded="Window_Loaded">
    <Grid>

    </Grid>
</Window>

MainWindow.xaml.cs:

public partial class MainWindow : Window, INotifyPropertyChanged
{
    public static readonly DependencyProperty MyTitleProperty = DependencyProperty.Register("MyTitle", typeof(String), typeof(MainWindow));

    public String MyTitle
    {
        get { return (String)GetValue(MainWindow.MyTitleProperty); }
        set
        {
            SetValue(MainWindow.MyTitleProperty, value);
            OnPropertyChanged("MyTitle");
        }
    }

    public MainWindow()
    {
        InitializeComponent();

        MyTitle = "Title";
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(String propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
    }
}
like image 558
lace.john Avatar asked Mar 07 '12 14:03

lace.john


2 Answers

First off, you don't need INotifyPropertyChanged if you just want to bind to a DependencyProperty. that would be redundant.

You don't need to set DataContext either, that's for a ViewModel scenario. (look into the MVVM pattern whenever you get a chance).

Now your declaration of dependency property is incorrect, it should be:

public string MyTitle
        {
            get { return (string)GetValue(MyTitleProperty); }
            set { SetValue(MyTitleProperty, value); }
        }

        // Using a DependencyProperty as the backing store for MyTitle.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MyTitleProperty =
            DependencyProperty.Register("MyTitle", typeof(string), typeof(MainWindow), new UIPropertyMetadata(null));

Notice the UIPropertyMetadata: it sets the default value for your DP.

And lastly, in your XAML:

<Window ...
       Title="{Binding MyTitle, RelativeSource={RelativeSource Mode=Self}}"
       ... />
like image 165
Louis Kottmann Avatar answered Oct 24 '22 07:10

Louis Kottmann


public MainWindow()
{
    InitializeComponent();

    DataContext = this;

    MyTitle = "Title";
}

Then you just need in the XAML

Title="{Binding MyTitle}"

Then you don't need the dependency property.

like image 30
Scroog1 Avatar answered Oct 24 '22 08:10

Scroog1