Move LocationMemberPath to MapItemsControl

This commit is contained in:
ClemensFischer 2022-11-03 21:16:46 +01:00
parent 65c93d1a3e
commit d8656c99a4
3 changed files with 43 additions and 20 deletions

View file

@ -26,11 +26,6 @@ namespace MapControl
/// </summary> /// </summary>
public partial class MapItem : ListBoxItem public partial class MapItem : ListBoxItem
{ {
public static readonly DependencyProperty LocationMemberPathProperty = DependencyProperty.Register(
nameof(LocationMemberPath), typeof(string), typeof(MapItem),
new PropertyMetadata(null, (o, e) => BindingOperations.SetBinding(
o, LocationProperty, new Binding { Path = new PropertyPath((string)e.NewValue) })));
/// <summary> /// <summary>
/// Gets/sets MapPanel.AutoCollapse. /// Gets/sets MapPanel.AutoCollapse.
/// </summary> /// </summary>
@ -48,15 +43,6 @@ namespace MapControl
get => (Location)GetValue(LocationProperty); get => (Location)GetValue(LocationProperty);
set => SetValue(LocationProperty, value); set => SetValue(LocationProperty, value);
} }
/// <summary>
/// Path to a source property for binding the Location property.
/// </summary>
public string LocationMemberPath
{
get => (string)GetValue(LocationMemberPathProperty);
set => SetValue(LocationMemberPathProperty, value);
}
} }
/// <summary> /// <summary>
@ -64,6 +50,18 @@ namespace MapControl
/// </summary> /// </summary>
public partial class MapItemsControl : ListBox public partial class MapItemsControl : ListBox
{ {
public static readonly DependencyProperty LocationMemberPathProperty = DependencyProperty.Register(
nameof(LocationMemberPath), typeof(string), typeof(MapItemsControl), new PropertyMetadata(null));
/// <summary>
/// Path to a source property for binding the Location property of MapItem containers.
/// </summary>
public string LocationMemberPath
{
get => (string)GetValue(LocationMemberPathProperty);
set => SetValue(LocationMemberPathProperty, value);
}
protected override DependencyObject GetContainerForItemOverride() protected override DependencyObject GetContainerForItemOverride()
{ {
return new MapItem(); return new MapItem();
@ -74,6 +72,31 @@ namespace MapControl
return item is MapItem; return item is MapItem;
} }
protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
{
base.PrepareContainerForItemOverride(element, item);
if (LocationMemberPath != null && element is MapItem mapItem)
{
mapItem.SetBinding(MapItem.LocationProperty,
new Binding
{
Path = new PropertyPath(LocationMemberPath),
Source = item
});
}
}
protected override void ClearContainerForItemOverride(DependencyObject element, object item)
{
base.ClearContainerForItemOverride(element, item);
if (LocationMemberPath != null && element is MapItem mapItem)
{
mapItem.ClearValue(MapItem.LocationProperty);
}
}
public void SelectItems(Predicate<object> predicate) public void SelectItems(Predicate<object> predicate)
{ {
if (SelectionMode == SelectionMode.Single) if (SelectionMode == SelectionMode.Single)

View file

@ -13,7 +13,6 @@
<Style x:Key="PointItemStyle" TargetType="map:MapItem"> <Style x:Key="PointItemStyle" TargetType="map:MapItem">
<Setter Property="AutoCollapse" Value="True"/> <Setter Property="AutoCollapse" Value="True"/>
<Setter Property="LocationMemberPath" Value="Location"/>
<Setter Property="FontSize" Value="12"/> <Setter Property="FontSize" Value="12"/>
<Setter Property="Template"> <Setter Property="Template">
<Setter.Value> <Setter.Value>
@ -68,7 +67,6 @@
<Style x:Key="PushpinItemStyle" TargetType="map:MapItem"> <Style x:Key="PushpinItemStyle" TargetType="map:MapItem">
<Setter Property="AutoCollapse" Value="True"/> <Setter Property="AutoCollapse" Value="True"/>
<Setter Property="LocationMemberPath" Value="Location"/>
<Setter Property="HorizontalAlignment" Value="Center"/> <Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Bottom"/> <Setter Property="VerticalAlignment" Value="Bottom"/>
<Setter Property="Template"> <Setter Property="Template">
@ -99,10 +97,12 @@
<map:MapItemsControl ItemsSource="{Binding Points}" <map:MapItemsControl ItemsSource="{Binding Points}"
ItemContainerStyle="{StaticResource PointItemStyle}" ItemContainerStyle="{StaticResource PointItemStyle}"
LocationMemberPath="Location"
SelectionMode="Extended"/> SelectionMode="Extended"/>
<map:MapItemsControl ItemsSource="{Binding Pushpins}" <map:MapItemsControl ItemsSource="{Binding Pushpins}"
ItemContainerStyle="{StaticResource PushpinItemStyle}"/> ItemContainerStyle="{StaticResource PushpinItemStyle}"
LocationMemberPath="Location"/>
<map:Pushpin AutoCollapse="True" Content="N 53°30' E 8°12'"> <map:Pushpin AutoCollapse="True" Content="N 53°30' E 8°12'">
<map:Pushpin.Location> <map:Pushpin.Location>

View file

@ -20,7 +20,6 @@
<Style x:Key="PointItemStyle" TargetType="map:MapItem"> <Style x:Key="PointItemStyle" TargetType="map:MapItem">
<Setter Property="AutoCollapse" Value="True"/> <Setter Property="AutoCollapse" Value="True"/>
<Setter Property="LocationMemberPath" Value="Location"/>
<Setter Property="FontSize" Value="12"/> <Setter Property="FontSize" Value="12"/>
<Setter Property="Template"> <Setter Property="Template">
<Setter.Value> <Setter.Value>
@ -75,7 +74,6 @@
<Style x:Key="PushpinItemStyle" TargetType="map:MapItem"> <Style x:Key="PushpinItemStyle" TargetType="map:MapItem">
<Setter Property="AutoCollapse" Value="True"/> <Setter Property="AutoCollapse" Value="True"/>
<Setter Property="LocationMemberPath" Value="Location"/>
<Setter Property="HorizontalAlignment" Value="Center"/> <Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Bottom"/> <Setter Property="VerticalAlignment" Value="Bottom"/>
<Setter Property="Template"> <Setter Property="Template">
@ -106,10 +104,12 @@
<map:MapItemsControl ItemsSource="{Binding Points}" <map:MapItemsControl ItemsSource="{Binding Points}"
ItemContainerStyle="{StaticResource PointItemStyle}" ItemContainerStyle="{StaticResource PointItemStyle}"
LocationMemberPath="Location"
SelectionMode="Extended"/> SelectionMode="Extended"/>
<map:MapItemsControl ItemsSource="{Binding Pushpins}" <map:MapItemsControl ItemsSource="{Binding Pushpins}"
ItemContainerStyle="{StaticResource PushpinItemStyle}"/> ItemContainerStyle="{StaticResource PushpinItemStyle}"
LocationMemberPath="Location"/>
<map:Pushpin AutoCollapse="True" Content="N 53°30' E 8°12'"> <map:Pushpin AutoCollapse="True" Content="N 53°30' E 8°12'">
<map:Pushpin.Location> <map:Pushpin.Location>