0

I am trying to change the fill color of some MapPolyline with a binding. In my XAML code I have

<m:Map x:Name="myMap"
       CredentialsProvider="XXXXX"
       Mode="Road">
    <m:MapItemsControl ItemsSource="{Binding Devices}">
        <m:MapItemsControl.ItemTemplate>
            <DataTemplate>
                <m:MapPolyline Locations="{Binding Locations}"
                     Fill="{Binding Path=Fill}"
                     Stroke="Blue"
                     StrokeThickness="4"
                     Opacity="1"/>
            </DataTemplate>
        </m:MapItemsControl.ItemTemplate>
    </m:MapItemsControl>
</m:Map>

When I try to run the program I get the error A 'Binding' cannot be set on the 'Fill' property of type 'MapPolyline'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.

While searching I found this answer, which makes me think that I cannot do this, because of the way the MapPolyline control is defined. I tired to dig into the MapPolyline control and it seems that Fill is a PropertyPath, not a DependencyProperty.

Am I correct? Is there any other way that I can change the Fill property either with a binding or with some code?

Ottavio Campana
  • 4,088
  • 5
  • 31
  • 58
  • "it seems that Fill is a PropertyPath" - highly unlikely. It must be Brush. are we talking about [this MapPolyline](https://learn.microsoft.com/en-us/previous-versions/bing/wpf-control/hh709548(v%3Dmsdn.10))? I would create an attached DP with single purpose of binding and setting Fill on change – ASh May 27 '20 at 16:11
  • Yes, that MapPolyline. How do you ATTACH a new DP? Do you create a new class that extends MapPolyline? – Ottavio Campana May 27 '20 at 20:52
  • read about `DependencyProperty.RegisterAttached()`. register it with a callback and change Fill in callback. "Do you create a new class that extends MapPolyline" - it may be simpler - I would try – ASh May 27 '20 at 20:56

1 Answers1

0

Am I correct?

Yes, you are. The target of a binding must be a dependency property and Fill doesn't seem to be.

Is there any other way that I can change the Fill property either with a binding or with some code?

You could for example handle the Loaded event for the MapPolyline and dynamically set its Fill property:

private void OnLoaded(object sender, RoutedEventArgs e)
{
    MapPolyline mapPolyline = (MapPolyline)sender;
    if (mapPolyline.DataContext is YourViewModel viewModel)
    {
        mapPolyline = viewModel.Fill;
    }
}

XAML:

    <m:MapPolyline  Loaded="OnLoaded"
                    Locations="{Binding Locations}"
                    Fill="{Binding Path=Fill}"
                    Stroke="Blue"
                    StrokeThickness="4"
                    Opacity="1"/>
mm8
  • 163,881
  • 10
  • 57
  • 88
  • @OttavioCampana: Did you try this or what happened? Please accept the answer and vote it up if your issue has been solved. – mm8 Jun 03 '20 at 14:00