将DataMmpu中定义的ContextMenu绑定到命令

时间:2013-11-04 18:17:42

标签: contextmenu datatemplate

好的,这是我的问题:我在Style.xaml中定义了一个UserControl模板,就像这样

 <Style x:Key="ModulProfTemplate" TargetType="{x:Type UserControl}" >
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type UserControl}">
                <Border BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true" BorderThickness="{TemplateBinding BorderThickness}" DataContext="{Binding DataContext, RelativeSource={RelativeSource TemplatedParent}}">

                    <Border x:Name="border" BorderBrush="#004E6CA9" BorderThickness="1.5">
                        <Grid x:Name="Layout" Background="{Binding Background, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Margin="0.5" DataContext="{Binding}">                                
                            <Grid.ContextMenu>
                                <ContextMenu>
                                    <MenuItem Header="Disponibil" Command="{Binding GridContextMenuCommand}" CommandParameter="disp"/>
                                    <MenuItem Header="Indisponibil" Command="{Binding GridContextMenuCommand}" CommandParameter="indisp"/>
                                </ContextMenu>
                            </Grid.ContextMenu>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="0.497*"/>
                                <RowDefinition Height="0.5*"/>
                            </Grid.RowDefinitions>
                            <StackPanel Orientation="Horizontal" d:LayoutOverrides="Height" HorizontalAlignment="Center" VerticalAlignment="Center">
                                <TextBlock TextWrapping="Wrap" Text="{Binding SalaSaptPara, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                <TextBlock TextWrapping="Wrap" Text="{Binding MaterieSaptPara, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                            </StackPanel>
                            <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
                                <TextBlock TextWrapping="Wrap" Text="{Binding SalaSaptImpara, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                <TextBlock TextWrapping="Wrap" Text="{Binding MaterieSaptImpara, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="2,0,0,0"/>
                            </StackPanel>
                        </Grid>
                    </Border>
                </Border>                    
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="BorderBrush" TargetName="border" Value="#CA4E6CA9"/>                            
                    </Trigger>
                </ControlTemplate.Triggers>                    
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

问题是我不知道如何将命令绑定到menuitem。 UserControl的DataContext设置为ViewModel.Luni.M1,命令位于ViewModel DataContext中。所以我的问题是:我如何更改DataContext以便我可以绑定命令?

1 个答案:

答案 0 :(得分:0)

解决了,我所要做的就是在绑定到我的viewmodel的datacontext的边框上添加一个标签,然后使用PlacementTarget.Tag.MyCommand,如下所示:)

                        <Border x:Name="border" BorderBrush="#004E6CA9" BorderThickness="1.5" Tag="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=my:CadreDidacticeView, AncestorLevel=1}, Path=DataContext}">
                        <Border.ContextMenu>
                            <ContextMenu>
                                <MenuItem Header="Disponibil" Command="{Binding PlacementTarget.Tag.GridContextMenuCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}">
                                </MenuItem>
                            </ContextMenu>
                        </Border.ContextMenu>