Search This Blog

Wednesday, November 13, 2013

Changing DataGridRow Appearance on Mouse Events

WPF > DataGrid > DataGridRow > Style

Example: Change DataGridRow  appearance on Mouse Over



XAML:


<Window x:Class="WpfApplication2.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" Loaded="Window_Loaded">
    <Window.Resources>
        <Style TargetType="{x:Type DataGridRow}">
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="LightCyan" />
                    <Setter Property="FontSize" Value="30" />
                    <Setter Property="Foreground"   Value="Red" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>

    <Grid>
        <DataGrid x:Name="dg" HorizontalAlignment="Left" VerticalAlignment="Top" Height="310" Width="507"/>
    </Grid>
</Window>
C# Code:

    public class Person
    {
        public string Name { get; set; }
        public string Position { get; set; }
    }

    public class Persons : ObservableCollection<Person>
    {
        public Persons()
        {
            Add( new Person() { Name ="Person1", Position = "Manager" } );
            Add(new Person() { Name = "Person2", Position = "Programmer" });
            Add(new Person() { Name = "Person3", Position = "Programmer" });
            Add(new Person() { Name = "Person4", Position = "Admin" });
            Add(new Person() { Name = "Person5", Position = "Tester" });
        }
    }

   dg.ItemsSource = new Persons();