0

我得到了 Silverlight 地图控件,我想访问 BoundingRectangle 属性。但这不是依赖属性。所以我的强项是创建一个附加属性,该属性绑定到我的 ViewModel 中的一个属性。并且每次调用此属性时,DepdendencyProperty Getter 都应返回地图元素的 BoundingRectangle 属性。但遗憾的是,没有调用 Getter...

这是我的代码

public class MapHelper
{
    public static readonly DependencyProperty MapViewRectangleProperty =
        DependencyProperty.RegisterAttached(
            "MapViewRectangle",
            typeof(LocationRect),
            typeof(MapHelper),
            new PropertyMetadata(null, new PropertyChangedCallback(MapViewRectanglePropertyChanged))
        );

    private static void MapViewRectanglePropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
        string balls = "balls";
    }

    public static void SetMapViewRectangle(object element, LocationRect value)
    {
        string balls = "balls";
    }

    public static LocationRect GetMapViewRectangle(object element)
    {
        if (element is Map)
        {
            return (LocationRect)(((Map)element).TargetBoundingRectangle);
        }
        else
        {
            return null;
        }
    }
}

XAML:

<m:Map utils:MapHelper.MapViewRectangle="{Binding Path=BoundingRectangle}" />

视图模型:

public LocationRect BoundingRectangle
    {
        get;
        set;
    }

我希望你能帮帮我 :)

4

1 回答 1

1

好的,另一次我对自己回答:D

在 ViewModel 中将我的附加属性绑定到我的属性

utils:MapHelper.MapViewRectangle="{Binding Path=BoundingRectangle,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"

创建了一个行为:

public class MapBoundingRectangleBehavior : Behavior<Map>
{
    protected override void OnAttached()
    {
        AssociatedObject.TargetViewChanged += new EventHandler<MapEventArgs>(AssociatedObject_TargetViewChanged);
    }

    void AssociatedObject_TargetViewChanged(object sender, MapEventArgs e)
    {
        AssociatedObject.SetValue(MapHelper.MapViewRectangleProperty, AssociatedObject.TargetBoundingRectangle);
    }
}

并将行为添加到地图控件:

<i:Interaction.Behaviors>
            <behaviors:MapBoundingRectangleBehavior />
        </i:Interaction.Behaviors>

听起来很简单,但它是唯一能够始终为我提供 BoundingRectangle 的正确数据的解决方案!

我希望我可以帮助遇到同样问题的任何人。

问候乔尼

于 2012-10-15T14:41:03.573 回答