ContextMenu将多个参数传递给viewmodel

时间:2017-04-19 17:26:53

标签: c# wpf mvvm

我有一个绑定到项目列表的画布。每个项目都有自己的X和Y字段,并在画布上绘制为矩形。

每个项目都有其contextmenu,在这种情况下绑定到List并动态填充(例如:“On”,“Off”)。

我现在正试图传递发送者(分配了Contextmenu的项目)和绑定的字符串作为CommandParameter视图模型。

例如: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>

1 个答案:

答案 0 :(得分:0)

对于提出不清楚或无用的问题感到抱歉:-)。无论如何,我自己找到了解决方案。

命令参数应该如下所示:

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