2017-04-19 177 views
-1

我有一個綁定到項目列表的畫布。每個項目都有自己的X和Y字段,並在畫布上繪製爲矩形。ContextMenu將多個參數傳遞給viewmodel

每個項目都有其上下文菜單,在這種情況下,它將被綁定到列表並被動態填充(例如:「開」,「關」)。

我現在試圖通過發件人(項目的上下文菜單分配給)和綁定的字符串作爲CommandParameter viewmodel。

例如:itemA,「On」

我應該怎麼做?

這裏是我的代碼:

  <ItemsControl 
       x:Name="Overlay" 
       Grid.Column="1" 
       GCextAp:Dragging.IsDragging="{Binding IsDragging, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" 
       ItemsSource="{Binding Path=MapElements, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"> 

       <ItemsControl.ItemsPanel> 
        <ItemsPanelTemplate> 
         <Canvas 
          localAp:MapProperties.GenerateMapElementFunc="{Binding CreateMapElementFunc}" 
          localAp:MapProperties.IsEditingMode="{Binding IsEditMode}" 
          localAp:MapProperties.ManipulationFinished="{Binding ManipulationFinishedDelegate}" 
          localAp:MapProperties.ScaleFactor="{Binding ElementName=Overlay, Path=DataContext.ScaleFactor}" 
          AllowDrop="True" 
          RenderOptions.BitmapScalingMode="LowQuality"> 

          <Canvas.Style> 
           <Style TargetType="Canvas"> 
            <Setter Property="Effect"> 
             <Setter.Value> 
              <DropShadowEffect 
               BlurRadius="8" 
               Direction="270" 
               ShadowDepth="2.5" 
               Color="#DDDDDD" /> 
             </Setter.Value> 
            </Setter> 
            <Setter Property="Opacity" Value="1" /> 
            <Setter Property="Background" Value="{x:Null}" /> 
            <Style.Triggers> 
             <MultiDataTrigger> 
              <MultiDataTrigger.Conditions> 
               <Condition Binding="{Binding ElementName=Overlay, Path=(GCextAp:Dragging.IsDragging)}" Value="true" /> 
               <Condition Binding="{Binding IsEditMode}" Value="true" /> 
              </MultiDataTrigger.Conditions> 
              <Setter Property="Background" Value="WhiteSmoke" /> 
              <Setter Property="Opacity" Value="0.1" /> 
             </MultiDataTrigger> 

            </Style.Triggers> 
           </Style> 
          </Canvas.Style> 
          <i:Interaction.Behaviors> 
           <localBehave:MapCanvasDropBehavior /> 
          </i:Interaction.Behaviors> 
         </Canvas> 
        </ItemsPanelTemplate> 
       </ItemsControl.ItemsPanel> 
       <ItemsControl.ItemContainerStyle> 
        <Style TargetType="ContentPresenter"> 
         <Setter Property="Canvas.Left" Value="{Binding DynamicX}" /> 
         <Setter Property="Canvas.Top" Value="{Binding DynamicY}" /> 
        </Style> 
       </ItemsControl.ItemContainerStyle> 
       <ItemsControl.ItemTemplate> 
        <DataTemplate> 

         <Rectangle 
          Width="{Binding DynamicWidth}" 
          Height="{Binding DynamicHeight}" 
          Stroke="Black" 
          Tag="{Binding DataContext, RelativeSource={RelativeSource AncestorType=Canvas}}" 
          Visibility="{Binding IsVisible, Converter={StaticResource converter}}"> 

          <Rectangle.Fill> 
           <ImageBrush ImageSource="{Binding Image}" /> 
          </Rectangle.Fill> 

          <i:Interaction.Behaviors> 
           <localBehave:MapElementMoveBehavior /> 
          </i:Interaction.Behaviors> 

          <Rectangle.ContextMenu> 
           <ContextMenu> 

            <MenuItem Header="Commands" ItemsSource="{Binding Path=PlacementTarget.Tag.AvailableElementCommands, RelativeSource={RelativeSource AncestorType=ContextMenu}, UpdateSourceTrigger=PropertyChanged}"> 
             <MenuItem.ItemContainerStyle> 
              <Style TargetType="{x:Type MenuItem}"> 
               <Setter Property="Header" Value="{Binding}" /> 
               <Setter Property="Command" Value="{Binding Path=PlacementTarget.Tag.CMD_MapElement, RelativeSource={RelativeSource AncestorType=ContextMenu}}" /> 
               <Setter Property="CommandParameter" Value=" I have no idea" /> 

              </Style> 
             </MenuItem.ItemContainerStyle> 

            </MenuItem> 


           </ContextMenu> 

          </Rectangle.ContextMenu> 

         </Rectangle> 


        </DataTemplate> 
       </ItemsControl.ItemTemplate> 
      </ItemsControl> 

回答

0

對不起問這是不清楚或有用:-)的問題。無論如何,我自己找到了解決方案。

命令paramater應該是這樣的:

<Setter Property="CommandParameter"> 
                <Setter.Value> 
                 <MultiBinding Converter="{StaticResource menuItemCommandConverter}"> 
                  <MultiBinding.Bindings> 
                   <Binding Path="DataContext" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}" /> 
                   <Binding Path="Header" RelativeSource="{RelativeSource Mode=Self}" /> 
                  </MultiBinding.Bindings> 
                 </MultiBinding> 
                </Setter.Value> 
               </Setter>