2017-05-23 7 views
0

私はWPF上にマップを持っています。ここでユーザーはダブルクリックしてポイントを選択できます。ただし、ユーザーが右クリックするとcontextMenu(または同様のもの)が開き、マーカーの名前が表示されます(マーカーは、地図上で緯度、経度でユーザーが選択したポイントの定義クラスです) &名前属性)。動的コンテキストメニューXAMLアイテムがobservablecollectionに配置されています

これは私がMarkersクラスの観測可能なコレクションを作成したが、それをContextMenuにバインドできないという主な考えです。だから、私は、マップ上のユーザーが右クリックすると、メニューにマーカーのすべての名前が表示され、ユーザーがそれらをクリックすると、いくつかの方法が使用されることを望みます。

これらのダイナミックアイテムをすべてバインドし、クリックすると表示されるすべてのアイテムのメソッドを作成できるようにするにはどうすればよいですか?ユーザーはいつでもマーカーを選択して更新する必要があります。

前もってありがとうございます、私は数週間これに固執しています。ここで

、それがコード示されています:

MapMarkerクラス:

{  class MapMarker 
    { 
    public string Name { get;set;} // Marker name 
    public double Latitude { get; set;} // Latitude coordinate 
    public double Longitude { get; set; } // Longitude coordinate 
    public int Posicio { get; set; } // Mapmarker location inside the list of pushpins 
    public Image Icona { get; set; } // Mapmarker icon 
    public string Comanda { get; set; } // Command when clicked 
    } 
} 

監視可能なコレクションクラス:.csファイルの背後にある

class OCollMapMarker : ObservableCollection<MapMarker> 
{ 
    public MapMarker Mmarker { get; set; } 
} 

コード:(Fullfilling観察可能なコレクション)

   MapMarker mpker= new MapMarker(); 
       mpker.Name = nameppin; 
       mpker.Posicio = poslist; 
       mpker.Latitude = valuep.Location.Latitude; 
       mpker.Longitude = valuep.Location.Longitude; 
       mpker.Icona = iconap; 
       mpker.Comanda = comanpin; 

       PushpCollect.Add(mpker); 

XAML:PushpCollectパブリックプロパティ(but notフィールド)である場合

<WPF:Map.ContextMenu> 
      <ContextMenu Name="Rclik" #####BINDHERE#####> 
       <ContextMenu.Style> 
        <Style TargetType="{x:Type ContextMenu}"> 
         <Setter Property="OverridesDefaultStyle" Value="False"/> 
         <Setter Property="Foreground" Value="White"/> 
         <Setter Property="Background" Value="Transparent"/> 
         <Setter Property="BorderBrush" Value="Transparent"/> 
        </Style> 
       </ContextMenu.Style> 
+0

ItemsSourceプロパティを任意のIEnumerableにバインドすることができます。 mm8

+0

@ mm8今は試しましたが、それは何もしません。実際、マップにはコンテキストメニューは表示されません。ありがとう2 – Aleix19

+1

ビューモデルクラスを持っていますかマップコントロールのDataContextは何ですか? – mm8

答えて

0

を(私はバインドする必要がありますが、私は、複数のと動作しないと考えて試してみましたのはここです)ウィンドウのコードビハインドで定義されているように、このようにバインドできるはずです:

<ContextMenu Name="Rclik" ItemsSource="{Binding PushpCollect, RelativeSource={RelativeSource AncestorType=Window}}"> 
    <ContextMenu.Style> 
     <Style TargetType="{x:Type ContextMenu}"> 
      <Setter Property="OverridesDefaultStyle" Value="False"/> 
      <Setter Property="Foreground" Value="White"/> 
      <Setter Property="Background" Value="Transparent"/> 
      <Setter Property="BorderBrush" Value="Transparent"/> 
     </Style> 
    </ContextMenu.Style> 
    <ContextMenu.ItemContainerStyle> 
     <Style TargetType="MenuItem"> 
      <Setter Property="Header" Value="{Binding Name}" /> 
     </Style> 
    </ContextMenu.ItemContainerStyle> 
</ContextMenu> 

https://stackoverflow.com/help/mcve

関連する問題