我在悬停在按钮上以及活动按钮的颜色上时效果有问题。如何实施?
主要问题是,当您悬停在按钮上时,活动按钮的颜色变为灰色,而不是保持紫色,我也不明白如何实现此功能,以便仅在活动按钮旁边出现紫色圆,并且当按钮单击并保留在那里时不会出现。
<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">
<StackPanel Orientation="Horizontal">
<Image x:Name="optimg" Visibility="{Binding ElementName=main, Path=Visibility}" 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">
<StackPanel Orientation="Horizontal">
<Image Source="{StaticResource Active_btn}" Visibility="Hidden" 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">
<StackPanel Orientation="Horizontal">
<Image Source="{StaticResource Active_btn}" Visibility="Hidden" 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>
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)
{
// Устанавливаем цвет активной кнопки
activeButton.Foreground = BtnActiveColor;
activeButton.IsEnabled = false;
// Обрабатываем остальные кнопки
foreach (var button in new[] { opt_sist, opt_zap, opt_game })
{
if (button != activeButton)
{
button.Foreground = BtnInactiveColor;
button.IsEnabled = true; // Остальные кнопки остаются кликабельными
}
}
}
private void Button_MouseEnter(object sender, MouseEventArgs e)
{
if (sender is Button button && button.IsEnabled)
{
button.Foreground = BtnHoverColor; // Меняем цвет на белый при наведении
}
}
private void Button_MouseLeave(object sender, MouseEventArgs e)
{
if (sender is Button button)
{
// Если кнопка активна, ничего не меняем
if (!button.IsEnabled) return;
// Возвращаем цвет к неактивному состоянию
button.Foreground = BtnInactiveColor;
}
}
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid x:Name="rootGrid">
<Grid.Resources>
<Style TargetType="{x:Type Button}">
<Setter Property="Foreground" Value="Purple"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<StackPanel Orientation="Horizontal" Background="{TemplateBinding Background}" Margin="{TemplateBinding Padding}">
<Image x:Name="optImg" Source="{StaticResource Active_btn}" Margin="0,0,5,0" Width="16" Height="16" Visibility="Hidden"/>
<ContentPresenter/>
</StackPanel>
<ControlTemplate.Triggers>
<Trigger Property="IsKeyboardFocused" Value="True">
<Setter Property="Visibility" Value="Visible" TargetName="optImg"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="Gray"/>
</Trigger>
</Style.Triggers>
</Style>
</Grid.Resources>
<StackPanel Orientation="Vertical" Width="100">
<Button Content="Button 1" Margin="5"/>
<Button Content="Button 2" Margin="5" IsEnabled="False"/>
<Button Content="Button 3" Margin="5"/>
</StackPanel>
</Grid>
</Page>