Defining a ContextMenu in a DataGridRow style

Posted by Brent on Stack Overflow See other posts from Stack Overflow or by Brent
Published on 2010-04-30T00:39:32Z Indexed on 2010/04/30 0:47 UTC
Read the original article Hit count: 737

Filed under:
|
|
|
|

I'm trying to clean up some of my xaml in my views by moving a lot of the DataGrid styles into a ResourceDictionary. One of the things I'd like to move is the ContextMenu that is bound to some commands in the ViewModel. However, when I move the context menu to the ResourceDictionary, the commands are are never firing anymore, and I can't figure out why.

I've defined the ContextMenu in the DataGridRow style so that when the user right clicks on the columnheader, no ContextMenu is shown... it will only be shown they right click on a row. Am I doing something wrong here?

FYI I'm using VS 2010 RTM if that makes a difference.

<Style x:Key="DataGridRowStyle" TargetType="{x:Type DataGridRow}">
    <Setter Property="Height" Value="20"/>
    <Setter Property="ContextMenu">
        <Setter.Value>
            <ContextMenu>
                <MenuItem Header="New" 
                          Command="{Binding RelativeSource={RelativeSource 
                          AncestorType=DataGrid}, Path=DataContext.NewCommand}">
                    <MenuItem.Icon>
                        <Image Source="/Images/DocumentWhite(32N).png" 
                               Width="16" 
                               Height="16"/>
                    </MenuItem.Icon>
                </MenuItem>
                <MenuItem Header="Open" 
                          Command="{Binding RelativeSource={RelativeSource 
                          AncestorType=DataGrid}, Path=DataContext.OpenCommand}">
                    <MenuItem.Icon>
                        <Image Source="/Images/FolderOpenYellow(32N).png" 
                               Width="16" 
                               Height="16"/>
                    </MenuItem.Icon>
                </MenuItem>
                <MenuItem Header="Delete" 
                          Command="{Binding RelativeSource={RelativeSource 
                          AncestorType=DataGrid}, Path=DataContext.DeleteCommand}">
                    <MenuItem.Icon>
                        <Image Source="/Images/Delete(32N).png" 
                               Width="16" 
                               Height="16"/>
                    </MenuItem.Icon>
                </MenuItem>
            </ContextMenu>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background" Value="{StaticResource hoverGradient}"/>
        </Trigger>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Background" Value="{StaticResource BtnOverFill}"/>
        </Trigger>
    </Style.Triggers>
</Style>

© Stack Overflow or respective owner

Related posts about wpf

Related posts about mvvm