You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WPF按钮悬停与激活状态样式异常技术求助

按钮悬停与激活样式修复方案

问题

  • 鼠标悬停激活状态的按钮时,颜色变为灰色,无法保持紫色
  • 紫色标识圆圈点击后残留显示,仅需在当前激活按钮旁展示

修复步骤

1. 解决激活按钮悬停变色问题

原代码通过IsEnabled = false标记激活状态,触发了WPF按钮默认的灰化样式,导致颜色异常。改为用变量跟踪激活按钮,保持按钮始终可用,避免默认样式干扰。

2. 解决紫色标识圆圈残留问题

原代码仅控制了第一个按钮的标识显示,需在激活切换时统一控制所有按钮的标识可见性:激活按钮显示标识,其余隐藏。


修改后的代码

XAML部分

<Button x:Name="razd_opt" Background="Transparent" BorderBrush="Transparent" FontFamily="{StaticResource Vela Sans M}" FontSize="16" Width="192" Height="31" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="26,22,0,0">
    <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" Margin="-62,0,0,0">
        <Image Source="{StaticResource Zap}" Width="18" Height="18" Margin="0,0,8,0"/>
        <TextBlock Text="Оптимизация" Style="{StaticResource style_btn_panel}" Foreground="#F5F1FF"/>
    </StackPanel>
</Button>

<Button x:Name="opt_sist" Background="Transparent" BorderBrush="Transparent" Width="212" Height="23" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="21,6,0,0" Click="opt_sist_Click" MouseEnter="Button_MouseEnter" MouseLeave="Button_MouseLeave">
    <StackPanel Orientation="Horizontal">
        <Image x:Name="activeImg_sist" Visibility="Visible" Source="{StaticResource Active_btn}" HorizontalAlignment="Left" Height="12" Width="12" Margin="-20 0 0 0" />
        <TextBlock x:Name="btn_1" Text="Оптимизация системы" Style="{StaticResource style_btn_panel}" FontFamily="{StaticResource Vela Sans M}" FontSize="14" Foreground="#725DFF"></TextBlock>
    </StackPanel>
</Button>

<Button x:Name="opt_zap" Background="#00FFFFFF" BorderBrush="Transparent" FontFamily="{StaticResource Vela Sans M}" FontSize="14" Width="212" Height="23" HorizontalAlignment="Left" VerticalAlignment="Top" TextElement.Foreground="#848386" Margin="18,6,0,0" Click="opt_zap_Click" MouseEnter="Button_MouseEnter" MouseLeave="Button_MouseLeave">
    <StackPanel Orientation="Horizontal">
        <Image x:Name="activeImg_zap" Visibility="Hidden" Source="{StaticResource Active_btn}" HorizontalAlignment="Left" Height="12" Width="12" Margin="-20 0 0 0"/>
        <TextBlock x:Name="btn_2" Text="Оптимизация запуска" Style="{StaticResource style_btn_panel}" FontFamily="{StaticResource Vela Sans M}" FontSize="14" Foreground="#848386"></TextBlock>
    </StackPanel>
</Button>

<Button x:Name="opt_game" Background="Transparent" BorderBrush="Transparent" FontFamily="{StaticResource Vela Sans M}" FontSize="14" Width="212" Height="23" HorizontalAlignment="Left" VerticalAlignment="Top" TextElement.Foreground="#848386" Margin="11,6,0,0" Click="opt_game_Click" MouseEnter="Button_MouseEnter" MouseLeave="Button_MouseLeave">
    <StackPanel Orientation="Horizontal">
        <Image x:Name="activeImg_game" Visibility="Hidden" Source="{StaticResource Active_btn}" HorizontalAlignment="Left" Height="12" Width="12" Margin="-20 0 0 0"/>
        <TextBlock x:Name="btn_3" Text="Game-оптимизация" Style="{StaticResource style_btn_panel}" FontFamily="{StaticResource Vela Sans M}" FontSize="14" Foreground="#848386"></TextBlock>
    </StackPanel>
</Button>

C#部分

private Button _currentActiveButton;
private readonly SolidColorBrush BtnActiveColor = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#725DFF"));
private readonly SolidColorBrush BtnInactiveColor = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#848386"));
private readonly SolidColorBrush BtnHoverColor = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#F5F1FF"));

private void opt_sist_Click(object sender, RoutedEventArgs e)
{
    main_sist.Visibility = Visibility.Visible;
    SetActiveButton(opt_sist);
}

private void opt_zap_Click(object sender, RoutedEventArgs e)
{
    SetActiveButton(opt_zap);
}

private void opt_game_Click(object sender, RoutedEventArgs e)
{
    SetActiveButton(opt_game);
}

private void SetActiveButton(Button activeButton)
{
    _currentActiveButton = activeButton;

    foreach (var button in new[] { opt_sist, opt_zap, opt_game })
    {
        var isActive = button == activeButton;
        button.Foreground = isActive ? BtnActiveColor : BtnInactiveColor;

        Image activeImg = button.Name switch
        {
            "opt_sist" => activeImg_sist,
            "opt_zap" => activeImg_zap,
            "opt_game" => activeImg_game,
            _ => null
        };
        activeImg?.SetValue(VisibilityProperty, isActive ? Visibility.Visible : Visibility.Hidden);
    }
}

private void Button_MouseEnter(object sender, MouseEventArgs e)
{
    if (sender is Button button && button != _currentActiveButton)
    {
        button.Foreground = BtnHoverColor;
    }
}

private void Button_MouseLeave(object sender, MouseEventArgs e)
{
    if (sender is Button button)
    {
        button.Foreground = button == _currentActiveButton ? BtnActiveColor : BtnInactiveColor;
    }
}

内容的提问来源于stack exchange,提问作者RoDeZ

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 20:45:54