在WPF中,如何在代码后台绑定依赖属性?

4

我有如下XAML代码:

<Ellipse x:Name="node" Height="10" Width="10" map:MapCanvas.Latitude="{Binding Latitude}" map:MapCanvas.Longitude="{Binding Longitude}"
                    Fill="Red" Stroke="Red" StrokeThickness="1" Stretch="Uniform" 
                </Ellipse>

并且这个

xmlns:map="clr-namespace:MapControl;assembly=MapControl"

这里是MapCanvas类中的依赖属性:

    public static readonly DependencyProperty LatitudeProperty =
        DependencyProperty.RegisterAttached("Latitude", typeof(double), typeof(MapCanvas), new PropertyMetadata(double.PositiveInfinity, OnLatitudeLongitudePropertyChanged));


    public static readonly DependencyProperty LongitudeProperty =
        DependencyProperty.RegisterAttached("Longitude", typeof(double), typeof(MapCanvas), new PropertyMetadata(double.PositiveInfinity, OnLatitudeLongitudePropertyChanged));

我知道之前有类似的问题,但我的问题是如何在代码中实现与XAML代码中经度和纬度相同的绑定?

1个回答

1
node.SetBinding(MapCanvas.LatitudeProperty, new Binding("Latitude"));

你可以像这样绑定...

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接