依赖属性Getter Call

时间:2012-10-15 10:11:37

标签: silverlight binding dependency-properties bing getter

我获得了Silverlight Map Control,我想访问BoundingRectangle属性。但它不是一个依赖属性。 所以我的想法是创建一个附加属性,该属性绑定到我的ViewModel中的属性。每次调用此属性时,DepdendencyProperty Getter都应返回Map元素的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;
    }

我希望你能帮助我:)。

1 个答案:

答案 0 :(得分: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的正确数据的解决方案!

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

问候 强尼