简体   繁体   中英

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?

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);

Yes, a canvas has no left property. it is a attached property for a FrameworkItem in a Canvas Content.

<Canvas Width="100" Height="100">
   <TextBox Name="top" Canvas.Left="12"></TextBox>
</Canvas>

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