0
votes

Ok, here is my problem: I have a UserControl Template defined in Style.xaml like this

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

the problem is that i don't know how to bind the command to menuitem. The UserControl's DataContext is set to ViewModel.Luni.M1, and the command is in ViewModel DataContext. So my question is: How do i change the DataContext so i can bind the command?

1

1 Answers

0
votes

Solved, all i had to do was add a tag to the border that was bound to my viewmodel's datacontext, and after that use PlacementTarget.Tag.MyCommand, as shown like this :)

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