0

I am trying to add a context menu to my WPF datagrid is specific to each row, because its items need to depend on the DataContext for that row. For example "Save" might be disabled if I know the row has not been edited.

I am looking at the accepted answer for Create contextmenus for datagrid rows and trying to adapt it to the existing xaml I am working with, but I don't know how to use this solution on top of my existing RowStyle.

If I copy and paste the context menu code everything works, but I already have this for RowStyle:

 <DataGrid.RowStyle>
   <Style TargetType="{x:Type DataGridRow}">
     <Style.Triggers>
       <Trigger Property="AlternationIndex" Value="1">
         <Setter Property="Background" Value="#eed3f7" />
       </Trigger>
   [...]
     </Style.Triggers>
   </Style>
 </DataGrid.RowStyle>

And I don't understand how to incorporate:

<DataGrid RowStyle="{StaticResource DefaultRowStyle}"/>

Please help!

Community
  • 1
  • 1
Imran
  • 12,950
  • 8
  • 64
  • 79

1 Answers1

1

I'm guessing that you've been looking at this for so long that you can't see the wood for the trees. In your linked answer, there is this Style:

<Style x:Key="DefaultRowStyle" TargetType="{x:Type DataGridRow}">
    <Setter Property="ContextMenu" Value="{StaticResource RowMenu}" />
</Style>

You say that you can't use it because you have your own Style that you want to apply... but the Style above has only one Setter and so there is nothing stopping you from copying that one Setter into your Style:

 <DataGrid.RowStyle>
   <Style TargetType="{x:Type DataGridRow}">
    <Setter Property="ContextMenu" Value="{StaticResource RowMenu}" /><!--<<<<<<<<-->
     <Style.Triggers>
       <Trigger Property="AlternationIndex" Value="1">
         <Setter Property="Background" Value="#eed3f7" />
       </Trigger>
   [...]
     </Style.Triggers>
   </Style>
 </DataGrid.RowStyle>

Alternatively, you could have based your Style on the DefaultRowStyle:

 <DataGrid.RowStyle>
   <Style TargetType="{x:Type DataGridRow}" BasedOn="DefaultRowStyle">
     <Style.Triggers>
       <Trigger Property="AlternationIndex" Value="1">
         <Setter Property="Background" Value="#eed3f7" />
       </Trigger>
   [...]
     </Style.Triggers>
   </Style>
 </DataGrid.RowStyle>
Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • Thank you! Yes I was just stabbing in the dark trying to guess the right syntax as it's my first time working with WPF. – Imran May 19 '14 at 22:04