0

I am trying to bind image source, the thing is that: I have a folder with a filesystemwatcher - each time a new image file is added to the folder - I'm getting the full path of the image and want to bind that to the source of the image control. So the images will change automatically in the GUI everytime a new TIFF file is added to the folder.

This is what I've done:

XAML:

<Window x:Class="WpfApplication2.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:WpfApplication2"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">

    <Grid>

        <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="414,159,0,0" VerticalAlignment="Top" Width="75" Click="button_Click"/>
        <Image x:Name="img1" HorizontalAlignment="Left" Height="100" VerticalAlignment="Top" Width="100" RenderTransformOrigin="1.604,1.161" Source="{Binding ButtonImage}" />

    </Grid>
</Window>

MainWindow.xaml.cs

private ImageSource b;

public  ImageSource ButtonImage
{
    get { return b; }
    set
    {
        b = value;
    }
}

private void button_Click(object sender, RoutedEventArgs e)
{
    ButtonImage = new BitmapImage(new Uri(@"C:\Users\x\Desktop\1.tif"));
    watch();
}

private void watch()
{
    FileSystemWatcher watcher = new FileSystemWatcher();
    watcher.Path = @"C:\Users\x\Desktop\ti";
    watcher.Created += new FileSystemEventHandler(OnChanged);
    watcher.EnableRaisingEvents = true;
    watcher.Filter = "*.tif";
}

private void OnChanged(object source, FileSystemEventArgs e)
{
    ButtonImage = new BitmapImage(new Uri(e.FullPath));
}
Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
  • _"I'm getting the full path of the image and want to bind that to the source of the image control"_ -- so why can't you? You should have a view model with a property for the path as the source, bound to the target `Source` property. Assuming you've implemented your view model correctly (you haven't, in the above example), updating the source property will automatically update the target property. – Peter Duniho Nov 11 '16 at 01:14
  • Without a good [mcve], it's impossible to know what's wrong. But the code you show doesn't implement `INotifyPropertyChanged` (see marked duplicate) and does not set the `DataContext` either. See also https://stackoverflow.com/questions/2036518/learning-wpf-and-mvvm, https://stackoverflow.com/questions/997650/i-have-some-questions-about-mvvm-pattern, https://stackoverflow.com/questions/1405739/mvvm-tutorial-from-start-to-finish, https://stackoverflow.com/questions/7063902/better-way-to-trigger-onpropertychanged, ... – Peter Duniho Nov 11 '16 at 01:39
  • ... https://stackoverflow.com/questions/772214/in-mvvm-should-the-viewmodel-or-model-implement-inotifypropertychanged, https://stackoverflow.com/questions/3130491/how-to-avoid-implementing-inotifypropertychanged-manually, https://stackoverflow.com/questions/488587/whats-the-best-way-to-call-inotifypropertychangeds-propertychanged-event, https://stackoverflow.com/questions/857820/big-smart-viewmodels-dumb-views-and-any-model-the-best-mvvm-approach, and https://stackoverflow.com/questions/6789236/how-does-wpf-inotifypropertychanged-work – Peter Duniho Nov 11 '16 at 01:39

1 Answers1

-1

Raise property changed event in the property setter

public  ImageSource ButtonImage
{
    get { return b; }
    set
    {
        b = value;
        PropertyChanged("ButtonImage");
    }
}

Create a viewmodel and move the property there and set the DataContext of your view to this viewmodel

If you are using code behind then don't use binding Give a name to the Image img.Source=value;

ChrisF
  • 134,786
  • 31
  • 255
  • 325