[英]ContextMenu passing multiple parameter to viewmodel
我有一個綁定到項目列表的畫布。 每個項目都有自己的X和Y字段,並在畫布上繪制為矩形。
每個項目都有其上下文菜單,在這種情況下,該菜單綁定到一個列表並動態填充(例如:“打開”,“關閉”)。
我現在正嘗試將發件人(將Contextmenu分配到的項目)和綁定字符串作為CommandParameter傳遞給viewmodel。
例如:itemA,“開”
我應該怎么做?
這是我的代碼:
<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>
很抱歉提出不清楚或無用的問題:-)。 無論如何,我自己找到了解決方案。
命令參數應如下所示:
<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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.