Re-enabled WPF dependency property value inheritance for MapPanel.ParentMap.

This commit is contained in:
ClemensF 2012-11-23 16:16:09 +01:00
parent afac46dcf1
commit 4fbc5bc00c
20 changed files with 177 additions and 79 deletions

View file

@ -108,6 +108,8 @@ namespace MapControl
public MapBase()
{
SetValue(MapPanel.ParentMapProperty, this);
Background = LightBackground;
TileLayers = new TileLayerCollection();
Initialize();

View file

@ -76,10 +76,13 @@
<Compile Include="MapGraticule.cs" />
<Compile Include="MapGraticule.Silverlight.WinRT.cs" />
<Compile Include="MapItem.cs" />
<Compile Include="MapItem.Silverlight.WinRT.cs" />
<Compile Include="MapItemsControl.cs" />
<Compile Include="MapItemsControl.Silverlight.WinRT.cs" />
<Compile Include="MapOverlay.cs" />
<Compile Include="MapOverlay.Silverlight.WinRT.cs" />
<Compile Include="MapPanel.cs" />
<Compile Include="MapPanel.Silverlight.WinRT.cs" />
<Compile Include="MapPolygon.cs" />
<Compile Include="MapPolyline.cs" />
<Compile Include="MapPolyline.Silverlight.cs" />
@ -88,6 +91,7 @@
<Compile Include="MercatorTransform.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Pushpin.cs" />
<Compile Include="Pushpin.Silverlight.WinRT.cs" />
<Compile Include="Tile.cs" />
<Compile Include="Tile.Silverlight.WinRT.cs" />
<Compile Include="TileContainer.cs" />

View file

@ -62,6 +62,7 @@
<Compile Include="MapItemsControl.cs" />
<Compile Include="MapItemsControl.WPF.cs" />
<Compile Include="MapPanel.cs" />
<Compile Include="MapPanel.WPF.cs" />
<Compile Include="MapPolyline.cs" />
<Compile Include="MapPolyline.WPF.cs" />
<Compile Include="MapTransform.cs" />

View file

@ -0,0 +1,15 @@
// XAML Map Control - http://xamlmapcontrol.codeplex.com/
// Copyright © 2012 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
namespace MapControl
{
public partial class MapItem
{
public MapItem()
{
DefaultStyleKey = typeof(MapItem);
MapPanel.AddParentMapHandlers(this);
}
}
}

View file

@ -22,12 +22,6 @@ namespace MapControl
public static readonly DependencyProperty LocationPathProperty = DependencyProperty.Register(
"LocationPath", typeof(string), typeof(MapItem), new PropertyMetadata(null, LocationPathPropertyChanged));
public MapItem()
{
MapPanel.AddParentMapHandlers(this);
DefaultStyleKey = typeof(MapItem);
}
/// <summary>
/// Gets or sets the property path that is used to bind the MapPanel.Location attached property.
/// </summary>

View file

@ -0,0 +1,21 @@
// XAML Map Control - http://xamlmapcontrol.codeplex.com/
// Copyright © 2012 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
#if WINRT
using Windows.UI.Xaml;
#else
using System.Windows;
#endif
namespace MapControl
{
public partial class MapItemsControl
{
public MapItemsControl()
{
DefaultStyleKey = typeof(MapItemsControl);
MapPanel.AddParentMapHandlers(this);
}
}
}

View file

@ -19,7 +19,7 @@ namespace MapControl
typeof(MapItemsControl), new FrameworkPropertyMetadata(typeof(MapItemsControl)));
}
partial void Initialize()
public MapItemsControl()
{
Items.CurrentChanging += OnCurrentItemChanging;
Items.CurrentChanged += OnCurrentItemChanged;

View file

@ -18,20 +18,6 @@ namespace MapControl
/// </summary>
public partial class MapItemsControl : ListBox
{
public MapItemsControl()
{
MapPanel.AddParentMapHandlers(this);
DefaultStyleKey = typeof(MapItemsControl);
Initialize();
}
partial void Initialize();
protected override DependencyObject GetContainerForItemOverride()
{
return new MapItem();
}
public UIElement ContainerFromItem(object item)
{
return item != null ? ItemContainerGenerator.ContainerFromItem(item) as UIElement : null;
@ -41,5 +27,10 @@ namespace MapControl
{
return container != null ? ItemContainerGenerator.ItemFromContainer(container) : null;
}
protected override DependencyObject GetContainerForItemOverride()
{
return new MapItem();
}
}
}

View file

@ -81,8 +81,9 @@ namespace MapControl
protected readonly Path Path = new Path();
protected readonly PathGeometry Geometry = new PathGeometry();
partial void Initialize()
public MapOverlay()
{
IsHitTestVisible = false;
Path.Stroke = Stroke;
Path.StrokeThickness = StrokeThickness;
Path.StrokeDashArray = StrokeDashArray;

View file

@ -69,10 +69,8 @@ namespace MapControl
typeof(MapOverlay), new FrameworkPropertyMetadata(false));
}
partial void Initialize()
public MapOverlay()
{
MapPanel.AddParentMapHandlers(this);
pen = new Pen
{
Brush = Stroke,

View file

@ -17,14 +17,6 @@ namespace MapControl
/// </summary>
public partial class MapOverlay
{
public MapOverlay()
{
IsHitTestVisible = false;
Initialize();
}
partial void Initialize();
public FontFamily FontFamily
{
get { return (FontFamily)GetValue(FontFamilyProperty); }

View file

@ -0,0 +1,69 @@
// XAML Map Control - http://xamlmapcontrol.codeplex.com/
// Copyright © 2012 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
#if WINRT
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
#else
using System.Windows;
using System.Windows.Media;
#endif
namespace MapControl
{
public partial class MapPanel
{
public static readonly DependencyProperty ParentMapProperty = DependencyProperty.RegisterAttached(
"ParentMap", typeof(MapBase), typeof(MapPanel), new PropertyMetadata(null, ParentMapPropertyChanged));
public MapPanel()
{
if (!(this is MapBase))
{
AddParentMapHandlers(this);
}
}
/// <summary>
/// Helper method to work around missing property value inheritance in Silverlight and WinRT.
/// Adds Loaded and Unloaded handlers to the specified FrameworkElement, which set and clear
/// the value of the MapPanel.ParentMap attached property.
/// </summary>
public static void AddParentMapHandlers(FrameworkElement element)
{
element.Loaded += (o, e) => GetParentMap(element);
element.Unloaded += (o, e) => element.ClearValue(ParentMapProperty);
}
public static MapBase GetParentMap(UIElement element)
{
var parentMap = (MapBase)element.GetValue(ParentMapProperty);
if (parentMap == null && (parentMap = FindParentMap(element)) != null)
{
element.SetValue(ParentMapProperty, parentMap);
}
return parentMap;
}
private static MapBase FindParentMap(UIElement element)
{
MapBase parentMap = null;
var parentElement = VisualTreeHelper.GetParent(element) as UIElement;
if (parentElement != null)
{
parentMap = parentElement as MapBase;
if (parentMap == null)
{
parentMap = GetParentMap(parentElement);
}
}
return parentMap;
}
}
}

View file

@ -0,0 +1,20 @@
// XAML Map Control - http://xamlmapcontrol.codeplex.com/
// Copyright © 2012 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using System.Windows;
namespace MapControl
{
public partial class MapPanel
{
public static readonly DependencyProperty ParentMapProperty = DependencyProperty.RegisterAttached(
"ParentMap", typeof(MapBase), typeof(MapPanel),
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.Inherits, ParentMapPropertyChanged));
public static MapBase GetParentMap(UIElement element)
{
return (MapBase)element.GetValue(ParentMapProperty);
}
}
}

View file

@ -26,30 +26,11 @@ namespace MapControl
/// The Location is transformed into a viewport position by the MapBase.LocationToViewportPoint
/// method and then applied to the RenderTransform as an appropriate TranslateTransform.
/// </summary>
public class MapPanel : Panel, IMapElement
public partial class MapPanel : Panel, IMapElement
{
public static readonly DependencyProperty ParentMapProperty = DependencyProperty.RegisterAttached(
"ParentMap", typeof(MapBase), typeof(MapPanel), new PropertyMetadata(null, ParentMapPropertyChanged));
public static readonly DependencyProperty LocationProperty = DependencyProperty.RegisterAttached(
"Location", typeof(Location), typeof(MapPanel), new PropertyMetadata(null, LocationPropertyChanged));
public MapPanel()
{
AddParentMapHandlers(this);
}
public static void AddParentMapHandlers(FrameworkElement element)
{
element.Loaded += (o, e) => element.SetValue(ParentMapProperty, FindParentMap(element));
element.Unloaded += (o, e) => element.ClearValue(ParentMapProperty);
}
public static MapBase GetParentMap(UIElement element)
{
return (MapBase)element.GetValue(ParentMapProperty);
}
public static Location GetLocation(UIElement element)
{
return (Location)element.GetValue(LocationProperty);
@ -83,8 +64,8 @@ namespace MapControl
SetViewportPosition(element, parentMap, location);
}
var frameworkElement = element as FrameworkElement;
var rect = new Rect(0d, 0d, element.DesiredSize.Width, element.DesiredSize.Height);
var frameworkElement = element as FrameworkElement;
if (frameworkElement != null)
{
@ -171,11 +152,6 @@ namespace MapControl
}
}
private static MapBase FindParentMap(DependencyObject obj)
{
return (obj == null || obj is MapBase) ? (MapBase)obj : FindParentMap(VisualTreeHelper.GetParent(obj));
}
private static void ParentMapPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
var element = obj as IMapElement;

View file

@ -2,7 +2,6 @@
// Copyright © 2012 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using System;
using System.Windows;
using System.Windows.Shapes;
@ -10,16 +9,17 @@ namespace MapControl
{
public partial class MapPolyline : Path
{
partial void Initialize()
public MapPolyline()
{
Data = Geometry;
MapPanel.AddParentMapHandlers(this);
}
protected override Size MeasureOverride(Size constraint)
{
return new Size(
Math.Max(Geometry.Bounds.Width, Geometry.Bounds.Right),
Math.Max(Geometry.Bounds.Height, Geometry.Bounds.Bottom));
// MeasureOverride in Silverlight occasionally tries to create
// negative width or height from a transformed geometry in Data.
return new Size(Geometry.Bounds.Width, Geometry.Bounds.Height);
}
}
}

View file

@ -8,9 +8,10 @@ namespace MapControl
{
public partial class MapPolyline : Path
{
partial void Initialize()
public MapPolyline()
{
Data = Geometry;
MapPanel.AddParentMapHandlers(this);
}
}
}

View file

@ -21,14 +21,6 @@ namespace MapControl
protected PathGeometry Geometry = new PathGeometry();
public MapPolyline()
{
MapPanel.AddParentMapHandlers(this);
Initialize();
}
partial void Initialize();
public LocationCollection Locations
{
get { return (LocationCollection)GetValue(LocationsProperty); }

View file

@ -0,0 +1,15 @@
// XAML Map Control - http://xamlmapcontrol.codeplex.com/
// Copyright © 2012 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
namespace MapControl
{
public partial class Pushpin
{
public Pushpin()
{
DefaultStyleKey = typeof(Pushpin);
MapPanel.AddParentMapHandlers(this);
}
}
}

View file

@ -22,12 +22,6 @@ namespace MapControl
public static readonly DependencyProperty LocationPathProperty = DependencyProperty.Register(
"LocationPath", typeof(string), typeof(Pushpin), new PropertyMetadata(null, LocationPathPropertyChanged));
public Pushpin()
{
MapPanel.AddParentMapHandlers(this);
DefaultStyleKey = typeof(Pushpin);
}
/// <summary>
/// Gets or sets the property path that is used to bind the MapPanel.Location attached property.
/// </summary>

View file

@ -132,9 +132,15 @@
<Compile Include="..\MapItem.cs">
<Link>MapItem.cs</Link>
</Compile>
<Compile Include="..\MapItem.Silverlight.WinRT.cs">
<Link>MapItem.Silverlight.WinRT.cs</Link>
</Compile>
<Compile Include="..\MapItemsControl.cs">
<Link>MapItemsControl.cs</Link>
</Compile>
<Compile Include="..\MapItemsControl.Silverlight.WinRT.cs">
<Link>MapItemsControl.Silverlight.WinRT.cs</Link>
</Compile>
<Compile Include="..\MapOverlay.cs">
<Link>MapOverlay.cs</Link>
</Compile>
@ -144,6 +150,9 @@
<Compile Include="..\MapPanel.cs">
<Link>MapPanel.cs</Link>
</Compile>
<Compile Include="..\MapPanel.Silverlight.WinRT.cs">
<Link>MapPanel.Silverlight.WinRT.cs</Link>
</Compile>
<Compile Include="..\MapPolygon.cs">
<Link>MapPolygon.cs</Link>
</Compile>
@ -165,6 +174,9 @@
<Compile Include="..\Pushpin.cs">
<Link>Pushpin.cs</Link>
</Compile>
<Compile Include="..\Pushpin.Silverlight.WinRT.cs">
<Link>Pushpin.Silverlight.WinRT.cs</Link>
</Compile>
<Compile Include="..\Tile.cs">
<Link>Tile.cs</Link>
</Compile>