locked
DataGrid cell MouseOver event underline and font colour too blue. RRS feed

  • Question

  • I am new to wpf and trying to recreate the look and feel of our existing Apps using WPF. I am stuck on one piece namely getting a cell of a datagrid to display in blue with an underline with a MouseOverEvent (Ihave got the colour to change but not the underline), changing the text decoration does not seem to work! Does anyone know how this can be achieved?

     

     

                 <DataGrid.CellStyle>

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

                        <Style.Triggers>

                            <Trigger Property="IsMouseOver" Value="True">

                                <Setter Property="TextBlock.Foreground" Value="Blue"/>

                                <Setter Property="TextBlock.TextDecorations" Value="Underline" />

                            </Trigger>

                        </Style.Triggers>

                    </Style>

                </DataGrid.CellStyle>

     

     

     

    Many thanks

    Shaun

    Sunday, July 10, 2011 5:06 PM

Answers

  • Hi Shaun,

    The DataGrid.CellStyle defines the styles for the DataGridCell objects. The Foreground property works because it is an attached property that can inherits. So it will also changes the Foreground of the elements in the DataGridCell's element tree.

    The TextDecorations property is not an attached property. You need to set it directly on the TextBlock.

    You can use DataGridTextColumn.ElementStyle to accomplish this.

    Here is a simple sample.

        <DataGrid ItemsSource="{Binding}" AutoGenerateColumns="False">
          <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Name}">
              <DataGridTextColumn.ElementStyle>
                <Style TargetType="TextBlock">
                  <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                      <Setter Property="Foreground" Value="Blue"/>
                      <Setter Property="TextDecorations" Value="Underline" />
                    </Trigger>
                  </Style.Triggers>
                </Style>
              </DataGridTextColumn.ElementStyle>
            </DataGridTextColumn>
          </DataGrid.Columns>
        </DataGrid>
    

    Best regards,


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Alacer dev Wednesday, July 13, 2011 2:04 PM
    Wednesday, July 13, 2011 6:06 AM

All replies

  • Hey Shaun, I hope you're fine.

    I've already seen this problem before and participated in its solution somehow and here's the post which got the problem solved in:

    http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/8ba94392-28ad-4c58-9c65-96d6711f4c92

    Please, mark my reply as "Answer" if it helps or vote it up if it gives a clue to the solution.


    Ahmed M. Gamil
    Sunday, July 10, 2011 5:26 PM
  • Thanks for your help, I did see that post when I was looking but I'm try to not use a hyper link column as we only want the hyper link to show when you hover over the cell in the grid. I'm sure this is possible using templates but unfortunately my wpf skills are not upto this at the moment.

     

    Shaun


    Shaun
    Sunday, July 10, 2011 7:19 PM
  • Hi Shaun,

    The DataGrid.CellStyle defines the styles for the DataGridCell objects. The Foreground property works because it is an attached property that can inherits. So it will also changes the Foreground of the elements in the DataGridCell's element tree.

    The TextDecorations property is not an attached property. You need to set it directly on the TextBlock.

    You can use DataGridTextColumn.ElementStyle to accomplish this.

    Here is a simple sample.

        <DataGrid ItemsSource="{Binding}" AutoGenerateColumns="False">
          <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Name}">
              <DataGridTextColumn.ElementStyle>
                <Style TargetType="TextBlock">
                  <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                      <Setter Property="Foreground" Value="Blue"/>
                      <Setter Property="TextDecorations" Value="Underline" />
                    </Trigger>
                  </Style.Triggers>
                </Style>
              </DataGridTextColumn.ElementStyle>
            </DataGridTextColumn>
          </DataGrid.Columns>
        </DataGrid>
    

    Best regards,


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Alacer dev Wednesday, July 13, 2011 2:04 PM
    Wednesday, July 13, 2011 6:06 AM
  • Thats the one thanks a lot for your help tried everything bar that it feels...

    Cheers


    Shaun
    Wednesday, July 13, 2011 2:04 PM