如何在代码后面实现以下xaml绑定?
<Canvas x:Name="_YAxis">
<Label Content="0.2" Canvas.Left="25" Canvas.Bottom="{Binding ElementName=_YAxis, Path=ActualHeight, Converter={StaticResource myPercentageOf}, ConverterParameter={StaticResource Constant_pt2} }" />
</Canvas>
注意,转换器只是将Canvas的实际高度乘以0.2
我可以理清大多数类型的绑定,但这个让我感到难过。
我可以使用
创建绑定Label label = new Label() { label.Content = "0.2" };
Binding binding = new Binding("ActualHeight");
binding.Source = _YAxis;
// attach binding ???
_YAxis.Children.Add(label);
但是如何将绑定附加到Canvas.Left附加属性?
答案 0 :(得分:3)
你走了:
Binding b = new Binding();
b.Path = new PropertyPath("ActualHeight");
b.Source = _YAxis;// OR b.ElementName = "_YAxis"
b.Converter = Resources["myPercentageOf"];
b.ConverterParameter = Resources["Constant_pt2"];
Label label = new Label() { label.Content = "0.2" };
_YAxis.Children.Add(label);
label.SetBinding(Canvas.BottomProperty, b); //Binding Canvas.Bottom to ActualHeight of _YAxis
Canvas.SetLeft(label, 25); //Setting Canvas.Left