为每个代码后面的附加属性添加DataBinding

Tal*_*dan 6 c# data-binding code-behind attached-properties

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

age*_*ped 18

你的问题有点不清楚,但我想你会问如何绑定附加属性Canvas.Left并在TextBox中显示它.我假设你想要它而不是TextBox的控件.

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

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

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

Binding binding = new Binding();
binding.Source = button;
binding.Path = new PropertyPath(Canvas.LeftProperty);
textBox.SetBinding(TextBlock.TextProperty, binding);
Run Code Online (Sandbox Code Playgroud)