【问题标题】:Binding to an attached property in code behind在后面的代码中绑定到附加属性
【发布时间】:2013-09-17 03:38:38
【问题描述】:

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

注意,转换器只是将画布的实际高度乘以 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 附加属性?

【问题讨论】:

    标签: wpf binding properties


    【解决方案1】:

    给你:

        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
    

    【讨论】:

    • 对于我的粗鲁没有感谢您的回复,我深表歉意。我不知道如何获取更新通知,所以没有看到您的回复。
    猜你喜欢
    • 2021-07-27
    • 1970-01-01
    • 2011-02-25
    • 2011-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-25
    相关资源
    最近更新 更多