I'm trying to change the background color of the XAML map pushpins using a DataTemplate trigger, but maybe there is something wrong in my code. The idea is that the ItemSource of the map is binded to an ObservableCollection of PushpinModel and when the value of the property IsOnline is true, then the pushpin should became green.
Here is my Geolocation.xaml
:
<m:Map CredentialsProvider="XXX" Mode="Road">
<m:MapItemsControl Name="Pushpins" ItemsSource="{Binding PushpinCollection}" >
<m:MapItemsControl.ItemTemplate>
<DataTemplate>
<m:Pushpin Location="{Binding Path=Location}" />
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Path=IsOnline}" Value="True">
<Setter Property="m:Pushpin.Background" Value="Green"></Setter>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</m:MapItemsControl.ItemTemplate>
</m:MapItemsControl>
</m:Map>
and here is the View Model GeolocationViewModel.cs
:
namespace MyNamespace
{
internal class Pushpins : ObservableCollection<PushpinModel> { }
internal class PushpinModel
{
public PushpinModel(double latitude, double longitude)
{
Location = new Location(latitude, longitude);
}
public Location Location { get; set; }
public bool IsOnline { get; set; } = false;
}
internal class GeolocationViewModel : INotifyPropertyChanged
{
public GeolocationViewModel()
{
pushpinCollection = new Pushpins();
CreatePushpins();
}
private Pushpins pushpinCollection;
public Pushpins PushpinCollection
{
get { return pushpinCollection; }
}
private void CreatePushpins()
{
Random rnd = new Random();
for (int i = 1; i <= 100; i++)
{
PushpinModel pin = new PushpinModel(rnd.NextDouble() * 180 - 90, rnd.NextDouble() * 360 - 180);
if (rnd.NextDouble() >= 0.5)
pin.IsOnline = true;
pushpinCollection.Add(pin);
}
OnPropertyChanged("PushpinCollection");
}
#region IPropertyChange
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
}
I have some doubts about the Property
of Setter
, cause I think I'm not changing the right item.
Any suggestions? Thank you guys!