当IsMouseOver = true时如何更改DataGridRow Foreground?

时间:2012-10-12 08:30:10

标签: wpf datagrid styles ismouseover

我在我的应用程序中遇到一个问题,即在datagridrow的IsMouseOver = true属性中更改背景颜色但我无法更改前景.. 检查属性我用过触发器.. 任何人都可以帮我解决它..

2 个答案:

答案 0 :(得分:0)

<Style TargetType="DataGridRow"> 
    <Style.Triggers> 
        <Trigger Property="IsMouseOver" Value="True"> 
             <Setter Property="Foreground" Value="Green" />
        </Trigger> 
    </Style.Triggers>
</Style> 

答案 1 :(得分:0)

要在鼠标结束时更改DataGridRow前景颜色,您必须使用样式触发器。这是如何做到这一点的一个例子。在此示例中,我将Foreground颜色设置为白色以使其更加明显。

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

代码背后:

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;
        }
    }
}

结果如下:
enter image description here