8

我想为附加的属性添加每个 Codebehind 的 DataBinding,并希望Canvas.Left在 TextBox 中显示该属性。如何添加此属性?

4

1 回答 1

21

您的问题有些不清楚,但我认为您是在问如何绑定到附加属性Canvas.Left并将其显示在 TextBox 中。我假设您希望它用于 TextBox 以外的控件。

<Canvas>
   <TextBox x:Name="textBox" Text="{Binding ElementName=button, Path=(Canvas.Left)}" />
   <Button x:Name="button" Content="Press me" />
</Canvas>

请注意附加属性周围的括号。

编辑:要在代码中执行等效操作,请使用以下命令:

Binding binding = new Binding();
binding.Source = button;
binding.Path = new PropertyPath(Canvas.LeftProperty);
textBox.SetBinding(TextBlock.TextProperty, binding);
于 2009-03-01T21:36:49.600 回答