WPF - Bind To Code Property in XAML
You defined following property in your Avalon Window's code:
Now you want to bind your property Special to a control:
But your TextBlock will stay cleared :( You can bind your TextBlock using Source Code but I prefer an other solution:
First: You can bind controls to element's properites.
e.g.
This would bind a TextBlocks Background color to stackpanel1's background color.
Second: This also works with an Avalon Window.
When you assign a Name to the Window you can bind properties of controls to the Avalon Window's properties.
eg:
Now you can bind your controls to any of your Window's properties.
Public Class Window1
...
Public Readonly Property Special As String
Return "Text"
End Property
End Class
Now you want to bind your property Special to a control:
<TextBlock Text="{DataBinding Path=Special}" />
But your TextBlock will stay cleared :( You can bind your TextBlock using Source Code but I prefer an other solution:
First: You can bind controls to element's properites.
e.g.
<TextBlock Background="{DataBinding Path=Background, ElementName=stackpanel1}" />
This would bind a TextBlocks Background color to stackpanel1's background color.
Second: This also works with an Avalon Window.
When you assign a Name to the Window you can bind properties of controls to the Avalon Window's properties.
eg:
<Window Name="w1" ... >
<TextBlock Text="{DataBinding Path=Special, ElementName=w1}" />
Now you can bind your controls to any of your Window's properties.
- Xaml Properties like ActualHeight, Width, Background etc.
- Code Properties like our Special sample
- MyApp's properties using the Window's MyApplication property
Comments