2017-07-26 14 views
2

DataTemplateトリガを使用してXAMLマップのプッシュピンの背景色を変更しようとしていますが、そのアイデアは、地図のItemSourceがPushpinModelのObservableCollectionにバインドされ、プロパティIsOnlineの値がtrueのときには、押ボタンが緑色になります。私は変更しないでいると思う原因私は、SetterPropertyに関するいくつかの疑問を持っているトリガを使用してXAML Mapのプッシュピンの背景色を変更します。

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 
    } 
} 

<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> 

、ここでは、ビューモデルGeolocationViewModel.csである:ここでは

は私Geolocation.xamlです右の項目。

提案がありますか?君たちありがとう!

+0

は、ビングマップで働いていないが、あなたはプッシュピン自体のスタイルを設定するのではなくするDataTemplateを指定して試してみました。プッシュピンのスタイルを使用すると、その機能をより詳細に制御でき、プッシュピンのカスタムアイコンを自分のような特定の条件に設定できるようになります。 –

答えて

0

最後に、Style.Triggersを使用して、プッシュピンの色を変更する方法を見つけました。ここ は作業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}" /> 
       <m:Pushpin.Style> 
        <Style TargetType="m:Pushpin"> 
         <Setter Property="Background" Value="Red" /> 
         <Style.Triggers> 
          <DataTrigger Binding="{Binding IsOnline}" Value="True"> 
           <Setter Property="Background" Value="Green" /> 
          </DataTrigger> 
         </Style.Triggers> 
        </Style> 
       <m:Pushpin.Style> 
      </DataTemplate> 
     </m:MapItemsControl.ItemTemplate> 
    </m:MapItemsControl> 
</m:Map> 

GeolocationViewModel.csの作業バージョン:

namespace MyNamespace 
{ 
    internal class Pushpins : ObservableCollection<Pushpin> { } 

    internal class Pushpin : INotifyPropertyChanged 
    { 
     public Pushpin(double latitude, double longitude) 
     { 
      Location = new Location(latitude, longitude); 
      IsOnline = false; 
     } 
     public Location Location { get; set; } 

     private bool isOnline; 
     public bool IsOnline 
     { 
      get { return isOnline; } 
      set 
      { 
       isOnline = value; 
       OnPropertyChanged("IsOnline"); 
      } 
     } 

     #region IPropertyChange 
     public event PropertyChangedEventHandler PropertyChanged; 

     protected void OnPropertyChanged(string propertyName) 
     { 
      PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
     } 
     #endregion 
    } 

    internal class GeolocationViewModel : INotifyPropertyChanged 
    { 
     public GeolocationViewModel() 
     { 
      pushpinCollection = new Pushpins(); 
      CreateRandomPushpins(); 
     } 

     private Pushpins pushpinCollection; 
     public Pushpins PushpinCollection 
     { 
      get { return pushpinCollection; } 
     } 

     private void CreateRandomPushpins() 
     { 
      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 
    } 
} 
関連する問題