wpfdatagridstylesismouseover

How to change DataGridRow Foreground when IsMouseOver= true?


i m facing one problem in my application that in datagridrow's IsMouseOver= true property changes background color but i m not able to change foreground.. To check property i have used trigger.. can anyone help me to solve it..


Solution

  • To change DataGridRow Foreground color when mouse is over, you have to use style trigger. This is an example of how to do it. In this example I set the Foreground color to white to make it more visible.

    XAML:

    <Window x:Class="PocWpf.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <Grid.Resources>
                <Style TargetType="{x:Type DataGridRow}" x:Key="GreenForegroundStyle">
                    <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Foreground" Value="White" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </Grid.Resources>
            <DataGrid x:Name="dgTest" AutoGenerateColumns="True" RowStyle="{StaticResource GreenForegroundStyle}">
    
            </DataGrid>
        </Grid>
    </Window>
    

    Code behind:

    namespace PocWpf
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
    
                this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
            }
    
            void MainWindow_Loaded(object sender, RoutedEventArgs e)
            {
                var list = new List<string>();
    
                list.Add("A");
                list.Add("A");
                list.Add("A");
                list.Add("A");
                list.Add("A");
                list.Add("A");
                list.Add("A");
                list.Add("A");
                list.Add("A");
                list.Add("A");
    
                this.dgTest.ItemsSource = list;
            }
        }
    }
    

    This is the result:
    enter image description here