1

如何在后面的代码中实现以下 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 附加属性?

4

1 回答 1

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
于 2013-09-12T16:38:52.553 回答