8

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?

Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
Taladan
  • 429
  • 1
  • 8
  • 20

1 Answers1

21

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);
Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
ageektrapped
  • 14,482
  • 7
  • 57
  • 72
  • 1
    Code binding does not work as in sample code. Third line should read like binding.Path = new PropertyPath(Canvas.Left); – Matze Apr 15 '10 at 14:28
  • Thank ageektrapped. But i search a way per codebehind, exactly per C#. I know the way per WPF. – Taladan Jan 17 '11 at 18:46