I want to add a DataBinding per Codebehind for an attached Property and want to show the Canvas.Left
property in a TextBox. How do I add this property?
Data binding is the process that establishes a connection between the app UI and the data it displays. If the binding has the correct settings and the data provides the proper notifications, when the data changes its value, the elements that are bound to the data reflect changes automatically.
Data binding is a mechanism in XAML applications that provides a simple and easy way for Windows Runtime Apps using partial classes to display and interact with data. The management of data is entirely separated from the way the data is displayed in this mechanism.
Binding path syntax. Use the Path property to specify the source value you want to bind to: In the simplest case, the Path property value is the name of the property of the source object to use for the binding, such as Path=PropertyName . Subproperties of a property can be specified by a similar syntax as in C#.
WPF data binding supports data in the form of CLR objects and XML. To provide some examples, your binding source may be a UIElement, any list object, a CLR object that is associated with ADO.NET data or Web Services, or an XmlNode that contains your XML data.
It's somewhat unclear from your question, but I think you're asking how one would bind to the attached property Canvas.Left
and show it in a TextBox. I'll assume you want it for a control other than the TextBox.
<Canvas>
<TextBox x:Name="textBox" Text="{Binding ElementName=button, Path=(Canvas.Left)}" />
<Button x:Name="button" Content="Press me" />
</Canvas>
Note the brackets around the attached property.
EDIT: To do the equivalent in code, use the following:
Binding binding = new Binding();
binding.Source = button;
binding.Path = new PropertyPath(Canvas.LeftProperty);
textBox.SetBinding(TextBlock.TextProperty, binding);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With