WPF binds a window title to a property

I am trying to bind a property value (MyTitle) of a class (MainWindow) that comes from a window. I created the MyTitleProperty dependency property, implemented the INotifyPropertyChanged interface, and modified the set MyTitle method to raise the PropertyChanged event by passing "MyTitle" as the property name parameters. I set MyTitle to "Title" in the constructor, but when the window opens, the title will be empty. If I set a breakpoint on the Loaded event, then Mytitle = "Title", but this.title = "". This, of course, is something incredibly obvious that I did not notice. 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) { } } 
+4
source share
3 answers
 public MainWindow() { InitializeComponent(); DataContext = this; MyTitle = "Title"; } 

Then you just need to in XAML

 Title="{Binding MyTitle}" 

Then you do not need the dependency property.

+20
source

First, you do not need INotifyPropertyChanged if you just want to bind to DependencyProperty . that would be superfluous.

You do not need to set the DataContext for either the ViewModel script. (check out the MVVM template whenever you get a chance).

Now your dependency property declaration 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)); 

Pay attention to UIPropertyMetadata : it sets the default value for your DP.

And finally, in your XAML:

 <Window ... Title="{Binding MyTitle, RelativeSource={RelativeSource Mode=Self}}" ... /> 
+6
source
 Title="{Binding Path=MyTitle, RelativeSource={RelativeSource Mode=Self}}" 
+3
source

Source: https://habr.com/ru/post/1400226/


All Articles