I have a problem with the effects when hovering over the button and with the color of the active button. How can this be implemented?
The main problem is that when you hover over the button, the color of the active button turns gray instead of remaining purple, I also don't understand how to implement this so that the purple circle appears only next to the active button, and does not appear when the button is clicked and remains there.
<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;
}
}
I have a problem with the effects when hovering over the button and with the color of the active button. How can this be implemented?
The main problem is that when you hover over the button, the color of the active button turns gray instead of remaining purple, I also don't understand how to implement this so that the purple circle appears only next to the active button, and does not appear when the button is clicked and remains there.
<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;
}
}
Share
Improve this question
asked Mar 15 at 10:43
RoDeZRoDeZ
751 silver badge6 bronze badges
1
- Use a set of RadioButtons instead; and style them accordingly. – Gerry Schmitz Commented Mar 15 at 16:37
1 Answer
Reset to default 0I am not 100% sure of what you are after but the comment is correct, it looks like you need to set up a Style with triggers for your buttons to achieve the appearance you want. If you have a dependency property for your notion of "IsActive" then you could bind to that instead of what I show here. Here I am using "IsKeyboardFocused" as a substitute for your notion of IsActive.
<Page xmlns="http://schemas.microsoft/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft/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>