I have a user control that has a Title property bound to the Text property of a TextBlock:
XAML
<TextBlock Grid.Column="0" Text="{Binding ElementName=me,Path=Title}" HorizontalAlignment="Left" VerticalAlignment="Top" />
Code behind
public String Title
{
get { return (String)GetValue(TitleProperty); }
set { SetValue(TitleProperty, value); }
}
public static readonly DependencyProperty TitleProperty =
DependencyProperty.Register("Title", typeof(String), typeof(ClosableTabHeader), new UIPropertyMetadata(""));
And I wish to bind this Title property to another property on the main window:
XAML
<my:ClosableTabHeader Title="{Binding ElementName=me,Path=ShortenAmount}" />
Code behind
public Int32 ShortenAmount
{
get { return (Int32)GetValue(ShortenAmountProperty); }
set { SetValue(ShortenAmountProperty, value); }
}
public static readonly DependencyProperty ShortenAmountProperty =
DependencyProperty.Register("ShortenAmount", typeof(Int32), typeof(MainWindow), new UIPropertyMetadata(0));
However, the textblock does not update.
When I replace the usercontrol declaration with a TextBlock, however, it works fine:
<TextBlock Text="{Binding ElementName=me,Path=ShortenAmount}" />
While there are certainly workarounds, in terms of what I am trying to do itself, may I know where I went wrong?
Problem lies in your
ElementName. From your codemy:ClosableTabHeaderis not named asme.melooks like yourMainWindow. But you are usingmeandTitletogether in this binding…where
Titleis property fromClosableTabHeaderand notMainWindow(wellMainWindow.Titleexists as a inbuilt WPF property but I am sure we are not talking about it)