我有以下代码来动态创建按钮并将其添加到面板:
StackPanel topPanel=...;
Button button=new Button();
button.Content="New Button "+topPanel.Children.Count;
// Set button background to a red/yellow linear gradient
// Create a default background brush
var bgBrush=new LinearGradientBrush(new GradientStopCollection(
new GradientStop[] {new GradientStop(Color.FromRgb(255,255,200),0.5),
new GradientStop(Color.FromRgb(255,200,200),0.5)}));
// Create a more intense mouse over background brush
var bgMouseOverBrush=new LinearGradientBrush(new GradientStopCollection(
new GradientStop[] {new GradientStop(Color.FromRgb(255,255,100),0.5),
new GradientStop(Color.FromRgb(255,100,100),0.5)}));
// Set the button's background
button.Background=bgBrush;
// Dynamically, add the button to the panel
topPanel.Children.Add(button);
问题是当我将鼠标光标移到按钮上时,它会恢复到以前的浅蓝色背景。现在,我已经读到我需要的是鼠标悬停按钮触发器,但我不知道如何仅针对这个按钮以编程方式执行此操作。基本上,我希望它的背景bgMouseOverBrush
在鼠标光标在它上面时改变,然后bgBrush
在它不在时改变。