如何在WPF中改变tableow的背景颜色

本文关键字:tableow 背景 颜色 改变 WPF | 更新日期: 2023-09-27 18:08:53

我创建了一个表格,当鼠标移动发生时,我想改变tablelow的背景颜色,当mouseleleave发生时,它会回到tablelow的旧颜色。这是代码,但它不能工作。

currentRow.MouseMove += new MouseEventHandler(ShowRowColor);
currentRow.MouseLeave += new MouseEventHandler(HideRowColor);
void ShowRowColor(object sender, System.Windows.Input.MouseEventArgs e){
        TableRow tr = sender as TableRow;
        ColorAnimation animation = new ColorAnimation();
        animation.From = (tr.Background as SolidColorBrush).Color;
        animation.To = Colors.Indigo;
        animation.Duration = new Duration(TimeSpan.FromSeconds(5));
        tr.BeginAnimation(SolidColorBrush.ColorProperty,animation);
    }
    void HideRowColor(object sender, System.Windows.Input.MouseEventArgs e) {
        TableRow tr = sender as TableRow;
        ColorAnimation animation;
        animation = new ColorAnimation();
        animation.From = Colors.Indigo;
        animation.To = (tr.Background as SolidColorBrush).Color;
        animation.Duration = new Duration(TimeSpan.FromSeconds(1));
        tr.BeginAnimation(SolidColorBrush.ColorProperty,animation);
    }

如何在WPF中改变tableow的背景颜色

您将希望在TableRowBackground属性上启动动画,因为您需要针对SolidColorBrushColor属性

的例子:

tr.Background.BeginAnimation(SolidColorBrush.ColorProperty, animation);