I have spent the day reading up on this and finally got a working example of some code which binds an image path to a binding source.
The problem is, I can only set it once when it first loads. After that I am unable to change the image to something different. I guess it is something to do with my implementation of inotifypropertychanged, but everything compiles and runs ok without errors.
Please could someone take a look at my code and suggest where I am going wrong?
The problem is, I can only set it once when it first loads. After that I am unable to change the image to something different. I guess it is something to do with my implementation of inotifypropertychanged, but everything compiles and runs ok without errors.
Please could someone take a look at my code and suggest where I am going wrong?
C#:
<Window x:Class="Introduction_to_WPF_Data_Binding.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:local="clr-namespace:Introduction_to_WPF_Data_Binding"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Image x:Name="image" Source="{Binding MyImagePath}" HorizontalAlignment="Left" Height="160" Margin="145,33,0,0" VerticalAlignment="Top" Width="224"/>
<Button x:Name="btnImage1" Content="Image 1" HorizontalAlignment="Left" Height="34" Margin="31,246,0,0" VerticalAlignment="Top" Width="75" Click="btnImage1_Click" />
<Button x:Name="btnImage2" Content="Image 2" HorizontalAlignment="Left" Height="34" Margin="145,246,0,0" VerticalAlignment="Top" Width="75"/>
<Button x:Name="btnImage3" Content="Image 3" HorizontalAlignment="Left" Height="34" Margin="256,246,0,0" VerticalAlignment="Top" Width="75"/>
<Button x:Name="btnImage4" Content="Image 4" HorizontalAlignment="Left" Height="34" Margin="367,246,0,0" VerticalAlignment="Top" Width="75" />
</Grid>
</Window>
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
namespace Introduction_to_WPF_Data_Binding
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = this; //Set Data Context
path = "img1.jpg"; //Set Default Image
}
string path = ""; //Create empty string for image path
public event PropertyChangedEventHandler PropertyChanged; //Event Handler for OnProperty Changed
private void OnPropertyChanged(string property)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
public string MyImagePath
{
get { return path; }
set
{
path = value;
OnPropertyChanged("MyImagePath");
}
}
private void btnImage1_Click(object sender, RoutedEventArgs e)
{
path = "img4.jpg";
}
}
}