문제

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..

도움이 되었습니까?

해결책

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

다른 팁

<Style TargetType="DataGridRow"> 
    <Style.Triggers> 
        <Trigger Property="IsMouseOver" Value="True"> 
             <Setter Property="Foreground" Value="Green" />
        </Trigger> 
    </Style.Triggers>
</Style> 
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top