Setting DataGridCell Style Overrides DataGridRow Style?

Sep 30, 2008 at 9:20 PM
I have created a DataGridRow Style and in it, set Background with an IsMouseOver trigger.

     <Style x:Key="rowStyle"
                TargetType="{x:Type toolkit:DataGridRow}">
        
         <Style.Triggers>
           
            <Trigger Property="IsSelected" Value="True">
               <Setter Property="Background" Value="{StaticResource listItemSelectedBrush}" />
            </Trigger>
           
            <MultiTrigger>
               <MultiTrigger.Conditions>
                  <Condition Property="IsMouseOver" Value="True" />
                  <Condition Property="IsSelected" Value="False" />
               </MultiTrigger.Conditions>
               <Setter Property="Background" Value="{StaticResource listviewItemMouseOverBrush}"/>
            </MultiTrigger>
 
         </Style.Triggers>
        
      </Style>

 I also have a DataGridCell Style which checks for IsSelected and also sets the BorderThickness.

     <Style x:Key="cellStyle"
             TargetType="{x:Type toolkit:DataGridCell}">

         <Setter Property="BorderThickness" Value="0" />

         <Style.Triggers>

            <Trigger Property="IsSelected" Value="True">
               <Setter Property="Foreground" Value="Black" />
               <Setter Property="Background" Value="{DynamicResource listItemSelectedBrush}" />
               <Setter Property="IsEditing" Value="True" />
            </Trigger>

         </Style.Triggers>

      </Style>

The problem is that if I set the Background in the "cellStyle", the IsMouseOver trigger on the "rowStyle" no longer works.  It makes sense to me that setting the Background at the Cell level will override whatever you set the Background to at the Row level, but then how do you hanlde MouseOvers for the Row?

Thanks in advance,

jcrain
Coordinator
Oct 2, 2008 at 2:48 PM
So you want the background for the selected cell to be both listItemSelectedBrush and listviewItemMouseOverBrush?  You cannot set a brush to both like that.  Or is there something else you want to do?
Oct 2, 2008 at 3:00 PM
Not quite ... I want the selected item brush to remain "listItemSelectedBrush" when the row is selected.  However, if the row is not selected and I mouse over it, I would like the background to have the "listviewItemMouseOverBrush".

Right now, if I add any kind of a background color in the cell style, whether the row is selected or not, the mouse over background brush will not apply.

Any thoughts?

Thanks,

James
Coordinator
Oct 3, 2008 at 2:13 PM
I tried your scenanio out and the row MouseOver seems to be working fine for me even when the DataGridCell was setting the background when it was selected.  The row IsSelected is getting overridden by the cell though (which is expected).
Oct 3, 2008 at 2:50 PM
Well that is good to hear.  Would you mind posting your solution so I could take a look? 

Thanks,

James
Coordinator
Oct 3, 2008 at 3:31 PM
It's basically the same as your solution (are you sure you're not overriding the background somewhere else?):

 

 

<Style TargetType="{x:Type dg:DataGridRow}">
    <Style.Triggers> 
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background" Value="Orange" />
        </Trigger> 
    </Style.Triggers>
</Style>

<
Style TargetType="{x:Type dg:DataGridCell}">
    <Style.Triggers> 
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Foreground" Value="Black" />
            <Setter Property="Background" Value="Blue" />
        </Trigger> 
    </Style.Triggers>
</Style>


 

Oct 6, 2008 at 8:35 PM
Oh, I guess maybe I misunderstood your last post. 

The DataGridCell "background" is being set in the Cell Style, so if you add a Setter to your DataGridCell for the background you should notice that your MouseOver for the DataGridRow will not work.

And that is my problem, that once the background for the cell is set (aside from when IsSelected = true) I want to still employ a MouseOver for the row but I can't because whatever the background is set to in the Cell Style takes precedence over the MouseOver setting.

If you don't mind, try that and let me know what you see.

Thanks,

James
Coordinator
Oct 7, 2008 at 2:13 AM
This is sort of a weird scenario.  Background set on the DataGridCell will always take precedence.  One thing you can possibly do is to attach to the DataGridRow MouseEnter event and from there change the DataGridCell.Background but that does seem like more of a hack.  Why don't you just create a trigger on the DataGridCell.MouseOver and set the background color to the same thing as listviewItemMouseOverBrush.
Oct 7, 2008 at 1:55 PM
Yeah, using the DataGridCell.MouseOver does the trick.   That surprises me because when I checked the MSDN knowledge base it said the DataGridCell didn't have a MouseOver; guess I should have tried it anyway huh.

Well thanks a lot for taking the time to help.  That solves my problem.

James


Coordinator
Oct 7, 2008 at 6:29 PM
Hi James,

I'm glad you were able to solve your problem.  I'm not sure if you realize, but the documentation you linked to on MSDN is actually for the Silverlight DataGrid (to my knowledge, I don't believe there is any documentation for the WPF DataGrid posted yet).  The Silverlight DataGrid is a subset of WPF's, so while do share many of the same features, there are a few (such as DataGridCell's MouseOver) which you will only find in the WPF DataGrid.  Until we have official docs posted, I guess the rule of thumb would be when in doubt, try it out!  And of course always feel free to post questions for the team here. :-)

Thanks!
Samantha
Oct 7, 2008 at 6:43 PM
Thanks Samantha,

Yeah, I did notice it was for Silverlight, but I just assumed that behavior would be the same.  Regardless, next time I will just try it out.

Thanks again for your help and yours too Vinisbal. :-)

James