着色 wpf 复选框 C#

coloring wpf checkbox box C#

我想为 WPF 应用程序中的 CheckBox 框着色。我试着在 XAML:

中输入
<CheckBox x:Name="topintegral" Content="TOP Integral" `enter code here`
          RenderTransformOrigin="0.5,0.5" Width="188" Margin="94,105,68,86"
          FontWeight="Bold" FontSize="15" Background="Black"
          MouseEnter="MouseFocus" MouseLeave="MouseLeave" BorderBrush="#FFE6FFFF">

并且在 .cs 文件中:

private void MouseFocus(object sender, MouseEventArgs e)
{
    topintegral.Background = new SolidColorBrush(Colors.White);
}

但显示如下:

enter image description here

如果你想让它回到以前的状态(黑色),那么在 MouseLeave 事件中做。

    private void MouseLeave(object sender, MouseEventArgs e)
    {
        topintegral.Background = new SolidColorBrush(Colors.Black);
    }

否则,您的代码工作正常。

MouseFocus 方法是MouseEnter 事件的事件处理程序,所以当光标进入复选框时,它变成白色。但是如果你想让它恢复到以前的颜色,只需编写 MouseLeave 事件的事件处理程序即可。