0

I got the Silverlight Map Control and I want to access the BoundingRectangle Property. But it's not a dependencyproperty. So my tought was to create an attaches Property which is bound to a property in my ViewModel. And everytime this Property is called the DepdendencyProperty Getter should return the BoundingRectangle Property of the Map Element. But sadly the Getter isn't called...

Heres my Code

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}" />

ViewModel:

public LocationRect BoundingRectangle
    {
        get;
        set;
    }

I hope you can help me :)

Johannes Wanzek
  • 2,825
  • 2
  • 29
  • 47

1 Answers1

1

Ok, another time I anwer to myself :D

Bound my Attached Property to my Property in ViewModel

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

Created a Behavior:

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);
    }
}

And added the Behavior to the Map Control:

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

Sounds so easy, but it's the only solution that gave me always the corrent data for the BoundingRectangle!

I hope I can help anybody who got the same Problem.

Greetings Jonny

Johannes Wanzek
  • 2,825
  • 2
  • 29
  • 47