I'm trying to change the image in a WPF application in C# when I click on a button. The image is like the background of the button, but I can't figure out how to change the image. Could someone give me some advice? I am new in WPF app btw.
My XAML button code:
<Window x:Class="Peel_App.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:local="clr-namespace:Peel_App"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<Grid x:Name="grid" HorizontalAlignment="Left" Height="420" VerticalAlignment="Top" Width="120" RenderTransformOrigin="0.5,0.5">
<Grid.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF111556" Offset="0.069"/>
<GradientStop Color="#FF4440E7" Offset="0.256"/>
<GradientStop Color="#FFE420DA" Offset="0.744"/>
</LinearGradientBrush>
</Grid.Background>
<Grid.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Grid.RenderTransform>
<Button x:Name="btn_Menu" HorizontalAlignment="Left" VerticalAlignment="Top" Width="45" Height="40" Click="btn_Menu_Click" >
<Button.Background>
<ImageBrush/>
</Button.Background>
</Button>
<Button x:Name="btn_Menu_Continue" HorizontalAlignment="Left" VerticalAlignment="Top" Width="45" Height="40" BorderBrush="#FF111556" Click="btn_Menu_Continue_Click" >
<Button.Background>
<ImageBrush/>
</Button.Background>
</Button>
<Button x:Name="btn_Home" Content="" HorizontalAlignment="Left" Margin="0,117,0,0" VerticalAlignment="Top" Width="120" Height="44" Click="Button_Click" HorizontalContentAlignment="Center">
<Button.Background>
<ImageBrush ImageSource="home_Unselected.png" Stretch="None"/>
</Button.Background>
</Button>
Button for changing Image:
This is what I've tried
private void Button_Click(object sender, RoutedEventArgs e)
{
var brush = new ImageBrush();
brush.ImageSource = new BitmapImage(new Uri("home_Selected.png", UriKind.Relative));
btn_Home.Background = brush;
}