-2
<StackPanel x:Uid="Style_1" x:Name="ForAdsImagePath" Grid.Row="1" Orientation="Horizontal" Margin="0,17,0,0" Visibility="Collapsed" HorizontalAlignment="Center" VerticalAlignment="Stretch">
    <Image x:Name="img1" Source="http://xxx.xxx.xxx.com/www/deliver/xxx.php?zoneid=9&amp;cb=131441234131313&amp;n=a25d26ed" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" MouseLeftButtonDown="Image_MouseLeftButtonDown"/>             
</StackPanel>

How do I write code for binding an image source in C#.

I tried this step but it gets an error upon initializing:

Source="{Binding Path, Converter={StaticResource MyPathConverter}}"
private void MyPathConverter()
{
    try
    {
        string Path = @"http://qa-ads.transim.com/www/delivery/avw.php?zoneid=9&amp;cb=131441234131313&amp;n=a25d26ed";
        Image image = new Image();
        image.UriSource = new Uri(Path);
    }
    catch (Exception ex) { /*Ignoe Function*/ }
}
Reuben Mallaby
  • 5,740
  • 4
  • 47
  • 45
  • Possible duplicate of [Set WPF Image Source from a Hyper Link (from Internet)](https://stackoverflow.com/questions/10869966/set-wpf-image-source-from-a-hyper-link-from-internet) – Ferus7 Dec 15 '17 at 07:00
  • Can you show me how to do it the right way on binding an image in xaml and C# with the link? – BugFixerDev-Migz Dec 15 '17 at 07:08

1 Answers1

1

Define public property in ViewModel

public class YourViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    private string imageSource;

    public string ImageSource
    {
        get { return imageSource; }
        set
        {
            imageSource = value;
            RaisePropertyChanged(nameof(ImageSource));
        }
    }
}

see here for INotifyPropertyChanged

Xaml:

<Image Source="{Binding Path=ImageSource}"/>

Usage:

ImageSource = "http://qa-ads.transim.com/www/images/c94efa71e756c4920ac93560f9ce8520.jpg";

Note: dont forget to set DataContext to your viewModel.

this.DataContext = new YourViewModel()
Clemens
  • 123,504
  • 12
  • 155
  • 268
tabby
  • 1,878
  • 1
  • 21
  • 39
  • 1
    Note that setting `UpdateSourceTrigger=PropertyChanged` has no effect in a OneWay Binding. I've removed it. – Clemens Dec 15 '17 at 07:12
  • just bad habit of writing UpdateSourceTrigger Thanks for Edit :) – tabby Dec 15 '17 at 07:14
  • Sir What is this for --> this.DataContext = new yourViewModel() ?? – BugFixerDev-Migz Dec 15 '17 at 07:15
  • DataContext is the place where your view looks for Data.see here https://stackoverflow.com/questions/7262137/what-is-datacontext-for – tabby Dec 15 '17 at 07:17
  • 1
    To add a little precision, the DataContext property of a UI element provides the source object of any Binding where a source is not specified explicitly (by setting Source, RelativeSource or ElementName). So when you write `{Binding Path=ImageSource}`, the framework expects that there is an object with an ImageSource property assigned to the current DataContext. Moreover, the DataContext property value is inherited to child elements in the visual tree. So when you set the MainWindow's DataContext, this value will automatiaclly be available to the Image element. – Clemens Dec 15 '17 at 07:22