When I learned WPF I couldn´t figure out how to create a simple binding from my XAML to some property in my code-behind file. Now when I know I thought that I should share this small piece of knowledge and spare you the frustration that I had.
One way to create a binding is to bind to another element in your XAML-file. E.g. Text=”{Binding SomeProperty, ElementName=MyOtherElement}” where SomeProperty is a property in your MyOtherElement. MyOtherElement is the name (x:Name) of an element in your XAML-file. With this knowledge we just set a name on our root element (Window, Page, UserControl or whatever) x:Name="myRoot" and then refer to that name in ElementName. E.g Text="{Binding MyLocalProperty, ElementName=myRoot }". The MyLocalProperty is then a property in our code-behind file. This can be a standard property or a dependency property.
Download the sample project (BindToAPropertyDemo.zip).
Thanks! quick and helpful.
ReplyDeleteThanks Anders. You just made my day!
ReplyDeleteThank you! this is EXACTLY what i'm searching for! very useful
ReplyDeleteThanks!!! This is exactly what I was looking for!
ReplyDeleteThanks. i have been googling for a couple of hours..
ReplyDeleteThe link to http://www.bursjoo.se/userfiles/Media/BindToAPropertyDemo.zip is not working anymore..
ReplyDeleteTHANK YOU !!!!!!!!!!!!!!!!!
ReplyDeleteYou save my day !! :D
Where is the demo for this? I am trying my hardest to get this to work and it always fails. Your download link does not work anymore.
ReplyDeleteThank you much!
ReplyDelete