Reputation: 937
I am manually creating a TextBlock and binding it's Text property to the Title property of the window by typing in the XAML file and when I compile and run the application, the TextBlock does not contain any text. Here is the code I am entering in to the MainWindow.xaml file:
<TextBlock Text="{Binding Title, ElementName=window}"/>
However, if I use the "Create Data Binding..." option from the property window on the right hand side of Visual Studio 2013 and it puts the exact same code in but when I compile and run the program it works...
I know I can just do this to get it to work but I have a bunch of code that I like to copy and paste and if I do this in this situation it doesn't work.
This seems to be the only property that does this on the window because I can bind to the window's Icon, Background, and Foreground properties without issue.
Can anyone tell me what I am doing wrong or is this a bug in VS2013?
** MORE INFORMATION FOR FUTURE REFERENCE **
Apparently when you use the "Create Data Binding..." option from the Properties window it automatically adds x:Name="window"
to the code for the Window control. This explains why it works when using the menu and not when typing it in.
Upvotes: 0
Views: 402
Reputation: 45106
set the name and refer to the name
not making it up - I do this
but I have not tested Title
<Window ...
x:Name="_this"
Upvotes: 0
Reputation: 16148
ElementName
is just that: the name of the element. Try setting it in parent window itself with x:Name="window'"
.
Upvotes: 2