C
C#3mo ago
!Rushaan

button background not changing wpf

I have a very simple but extremely annoying issue, ive assigned 4 buttons with the same mouse enter event and trying to change their background/foreground/border brush AND NOTHING IS CHANGING IT JUST STAYS THE SAME AS WHENEVER BUTTON IS NORMALLY HIGHLIGHTED! im 100% sure the event is triggered though as i checked
<Button Name="Option1" MouseEnter="Button_MouseEnter" MouseLeave="Button_MouseLeave" Background="Transparent" BorderBrush="Transparent" Margin="0 13 0 0" Padding="5 0 5 0" HorizontalAlignment="Center" VerticalAlignment="Top">
<Image Source="/Images/cdpr.png"></Image>

</Button>
<Button Name="Option2" MouseEnter="Button_MouseEnter" MouseLeave="Button_MouseLeave" Background="Transparent" BorderBrush="Transparent" Margin="0 90 0 0" Padding="5 0 5 0" HorizontalAlignment="Center" VerticalAlignment="Top">
<Image Source="/Images/c.png"></Image>
</Button>
<Button Name="Option3" MouseEnter="Button_MouseEnter" MouseLeave="Button_MouseLeave" Background="Transparent" BorderBrush="Transparent" Grid.RowSpan="2" Margin="0 140 0 0" Padding="5 0 5 0" HorizontalAlignment="Center" VerticalAlignment="Top">
<Image Source="/Images/w.png"></Image>
</Button>
<Button Name="Option4" MouseEnter="Button_MouseEnter" MouseLeave="Button_MouseLeave" Background="Transparent" BorderBrush="Transparent" Grid.RowSpan="2" Margin="0 190 0 0" Padding="5 0 5 0" HorizontalAlignment="Center" VerticalAlignment="Top">
<Image Source="/Images/g.png"></Image>
</Button>
<Button Name="Option1" MouseEnter="Button_MouseEnter" MouseLeave="Button_MouseLeave" Background="Transparent" BorderBrush="Transparent" Margin="0 13 0 0" Padding="5 0 5 0" HorizontalAlignment="Center" VerticalAlignment="Top">
<Image Source="/Images/cdpr.png"></Image>

</Button>
<Button Name="Option2" MouseEnter="Button_MouseEnter" MouseLeave="Button_MouseLeave" Background="Transparent" BorderBrush="Transparent" Margin="0 90 0 0" Padding="5 0 5 0" HorizontalAlignment="Center" VerticalAlignment="Top">
<Image Source="/Images/c.png"></Image>
</Button>
<Button Name="Option3" MouseEnter="Button_MouseEnter" MouseLeave="Button_MouseLeave" Background="Transparent" BorderBrush="Transparent" Grid.RowSpan="2" Margin="0 140 0 0" Padding="5 0 5 0" HorizontalAlignment="Center" VerticalAlignment="Top">
<Image Source="/Images/w.png"></Image>
</Button>
<Button Name="Option4" MouseEnter="Button_MouseEnter" MouseLeave="Button_MouseLeave" Background="Transparent" BorderBrush="Transparent" Grid.RowSpan="2" Margin="0 190 0 0" Padding="5 0 5 0" HorizontalAlignment="Center" VerticalAlignment="Top">
<Image Source="/Images/g.png"></Image>
</Button>
private void Button_MouseEnter(object sender, RoutedEventArgs e)
{
if (sender is Button button)
{
button.Background = Brushes.Pink;

Console.WriteLine("BUTTON ETNEENTTERR");
}
}

private void Button_MouseLeave(object sender, RoutedEventArgs e)
{
if (sender is Button button)
{
button.Background = Brushes.Transparent;
}
}
private void Button_MouseEnter(object sender, RoutedEventArgs e)
{
if (sender is Button button)
{
button.Background = Brushes.Pink;

Console.WriteLine("BUTTON ETNEENTTERR");
}
}

private void Button_MouseLeave(object sender, RoutedEventArgs e)
{
if (sender is Button button)
{
button.Background = Brushes.Transparent;
}
}
the only thing i can change about the button is isHitTestVisible
8 Replies
Mayor McCheese
Mayor McCheese3mo ago
In WPF you should use styles for this. I'll try and find a link
Buddy
Buddy3mo ago
Also please dont use the designer Nor should you use margins as the tool of doing layouts Use layout controls, such as DockPanel, Grid, StackPanel, etc.
!Rushaan
!Rushaan3mo ago
How exactly do u position elements exactly where u want with layout controls only I think I need a positioning guide
Buddy
Buddy3mo ago
Margin is the spacing between elements, not absolute position and even if it was, it would only be a mess if you resized your application https://wpf-tutorial.com/panels/introduction-to-wpf-panels/
!Rushaan
!Rushaan3mo ago
Yea im kinda feeling that
Mayor McCheese
Mayor McCheese3mo ago
@Azym Equinox here is an example of style triggers https://wpf-tutorial.com/styles/trigger-datatrigger-event-trigger/ NB: I don't see a date on the article itself, and this fits my recollection of using triggers, but syntax may be slightly different in latest versions of wpf. Here is an example for a really old repo that I did some amateur wpf work in... https://github.com/devdevdeveau/wpf-timebox/blob/main/TimeBox/MainWindow.xaml
!Rushaan
!Rushaan3mo ago
Alright but shouldn't changing properties of button in mouse enter event work too?
Mayor McCheese
Mayor McCheese3mo ago
It should, yeah, and my proposed fix probably won't work, you've got some ( on the surface ) incompatible features on the button that make it not work. If you get rid of the image content on your buttons it works as expected. If I copy/paste your code I have the same issue and if I reduce the xaml by removing images and replacing with text content it works as you'd expect.