diff --git a/MapControl/Shared/MapItemsControl.cs b/MapControl/Shared/MapItemsControl.cs index f5da9242..3275762b 100644 --- a/MapControl/Shared/MapItemsControl.cs +++ b/MapControl/Shared/MapItemsControl.cs @@ -26,11 +26,6 @@ namespace MapControl /// 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) }))); - /// /// Gets/sets MapPanel.AutoCollapse. /// @@ -48,15 +43,6 @@ namespace MapControl get => (Location)GetValue(LocationProperty); set => SetValue(LocationProperty, value); } - - /// - /// Path to a source property for binding the Location property. - /// - public string LocationMemberPath - { - get => (string)GetValue(LocationMemberPathProperty); - set => SetValue(LocationMemberPathProperty, value); - } } /// @@ -64,6 +50,18 @@ namespace MapControl /// public partial class MapItemsControl : ListBox { + public static readonly DependencyProperty LocationMemberPathProperty = DependencyProperty.Register( + nameof(LocationMemberPath), typeof(string), typeof(MapItemsControl), new PropertyMetadata(null)); + + /// + /// Path to a source property for binding the Location property of MapItem containers. + /// + public string LocationMemberPath + { + get => (string)GetValue(LocationMemberPathProperty); + set => SetValue(LocationMemberPathProperty, value); + } + protected override DependencyObject GetContainerForItemOverride() { return new MapItem(); @@ -74,6 +72,31 @@ namespace MapControl 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 predicate) { if (SelectionMode == SelectionMode.Single) diff --git a/SampleApps/UniversalApp/MainPage.xaml b/SampleApps/UniversalApp/MainPage.xaml index f3cb3e11..7f0a7280 100644 --- a/SampleApps/UniversalApp/MainPage.xaml +++ b/SampleApps/UniversalApp/MainPage.xaml @@ -13,7 +13,6 @@