Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add DataBinding for attached Property per Code Behind

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?

like image 648
Taladan Avatar asked Mar 01 '09 20:03

Taladan


People also ask

What is databinding C#?

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.

What is XAML data binding?

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.

What is binding path in WPF?

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#.

Which class is used for data binding in WPF?

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.


1 Answers

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);
like image 131
ageektrapped Avatar answered Sep 19 '22 17:09

ageektrapped