简体   繁体   中英

Binding TextBlock Text to Window Title doesn't work

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.

ElementName is just that: the name of the element. Try setting it in parent window itself with x:Name="window'" .

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM