2012-10-15 82 views
0

我得到了Silverlight地圖控件,我想訪問BoundingRectangle屬性。但它不是一個依賴屬性。 所以我的目標是創建一個附屬屬性,它綁定到ViewModel中的一個屬性。每次調用這個屬性時,DepdendencyProperty Getter應該返回Map Element的BoundingRectangle屬性。 但可悲的是,吸氣不叫......依賴屬性獲取調用

我的繼承人代碼

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

好吧,其他時間我前面回答自己:d

束縛我的附加屬性,以我的屬性,視圖模型

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> 

聽起來很容易,但它是唯一的解決方案,讓我總是co BoundingRectangle的rrent數據!

我希望我可以幫助任何人得到同樣的問題。

Greetings Jonny