我需要帮助编写延迟代码,以便在 mouse_hover 离开表格中的单元格时为其添加效果。当鼠标进入单元格时,它会将边框着色为绿色。当它离开细胞时,颜色必须在 1 秒内慢慢变回原来的颜色(银色)。在此期间,用户必须能够不间断地悬停在其他单元格上。这是我到目前为止所尝试的:
//cell hover effects
System.Timers.Timer timer = new System.Timers.Timer(1);
TableCell tc;
private void cell_MouseEnter(object sender, MouseEventArgs e)
{
tc = (TableCell)sender;
tc.BorderBrush = Brushes.Green;
}
private void cell_MouseLeave(object sender, MouseEventArgs e)
{
tc = (TableCell)sender;
timer.Start();
timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
tc.BorderBrush = Brushes.Silver;
}
private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
tc.BorderBrush = Brushes.Silver;
timer.Stop();
}
当我移动到另一个单元格时,此代码给我一个错误,因为另一个线程正在处理当前单元格。有人可以帮我解决这个问题或告诉我一个更好的方法来实现这个吗?
需要 C# 代码解决方案,而不是 XAML
完整代码供参考:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
InitTable();
}
private void InitTable()
{
//Canvas task1Canvas = new Canvas();
Table symptomTable = new Table();
//task1Canvas.Children.Add(symptomTable);
FlowDocument flowDoc = new FlowDocument();
flowDoc.Blocks.Add(symptomTable);
//symptomTable.CellSpacing = 128;
// Create N columns and add them to the table's Columns collection.
int numOfCols = 14;
for (int i = 0; i < numOfCols; i++)
{
symptomTable.Columns.Add(new TableColumn());
symptomTable.Columns[i].Width = new GridLength(128); //cell width
}
// Create and add an empty TableRowGroup Rows.
symptomTable.RowGroups.Add(new TableRowGroup());
//Add the first row to the table
symptomTable.RowGroups[0].Rows.Add(new TableRow());
//Configure the table head row
TableRow currentRow = symptomTable.RowGroups[0].Rows[0];
// Add the header row with content,
currentRow.Cells.Add(new TableCell(new Paragraph(new Run("August"))));
for (int n = 1; n <= 13; n++)
currentRow.Cells.Add(new TableCell(new Paragraph(new Run((10+n).ToString()))));
//Add the remaining rows
int row = 1;
string[] rowHeaders = new string[] { "river", "explosion", "flu", "airport", "chills", "morning", "tech", "truck", "cold" };
foreach (string label in rowHeaders)
{
symptomTable.RowGroups[0].Rows.Add(new TableRow());
//Configure the table head row
currentRow = symptomTable.RowGroups[0].Rows[row++];
// Add the header row with content,
currentRow.Cells.Add(new TableCell(new Paragraph(new Run(label))));
for (int n = 1; n <= 13; n++)
currentRow.Cells.Add(new TableCell(new Paragraph(new Run(""))));
for (int n = 1; n < currentRow.Cells.Count; n++)
{
currentRow.Cells[n].BorderThickness = new Thickness(3, 3, 3, 3);
currentRow.Cells[n].BorderBrush = Brushes.Silver;
currentRow.Cells[n].MouseEnter += new MouseEventHandler(cell_MouseEnter);
if(label != "chills")
currentRow.Cells[n].MouseLeave += new MouseEventHandler(cell_MouseLeave);
}
}
//Add the given flow document to the window
this.Content = flowDoc;
}
//cell hover effects
System.Timers.Timer timer = new System.Timers.Timer(1);
TableCell tc;
Thread animatationThread;
private void cell_MouseEnter(object sender, MouseEventArgs e)
{
tc = (TableCell)sender;
//Color colour = System.Drawing.ColorTranslator.FromHtml("#66CC00");
Color greenShade = (Color)ColorConverter.ConvertFromString("#FF66CC00");
BrushConverter converter = new BrushConverter();
Brush brush = converter.ConvertFromString("#FF66CC00") as Brush;
tc.BorderBrush = brush;
//System.Threading.Thread animationThread = new System.Threading.Thread();
//animatationThread = new Thread(new ThreadStart(brush.BeginAnimation(SolidColorBrush.ColorProperty, new System.Windows.Media.Animation.ColorAnimation(Colors.Silver, TimeSpan.FromSeconds(1)))));
//brush.BeginAnimation(SolidColorBrush.ColorProperty, new System.Windows.Media.Animation.ColorAnimation(Colors.Silver, TimeSpan.FromSeconds(1)));
}
private void cell_MouseLeave(object sender, MouseEventArgs e)
{
//TableCell tc;
tc = (TableCell)sender;
//timer.Start();
//timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
tc.BorderBrush = Brushes.Silver;
}
private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
tc.BorderBrush = Brushes.Silver;
timer.Stop();
}
}