Virender Prajapati
Virender Prajapati

Reputation: 13

WPF Dynamic Data Binding in Image source

What i m doing wrong?

Xaml:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="26*"/>
        <RowDefinition Height="63*"/>
        <RowDefinition Height="67*"/>
    </Grid.RowDefinitions>
    <TextBlock Name ="title" Text="" TextWrapping="NoWrap" Grid.Row="0" Margin="25,10,25,0"/>
    <Image Source="{Binding Path=BindImgURL}" HorizontalAlignment="Left" Height="164" Grid.Row="1" VerticalAlignment="Top" Width="306" Margin="152,0,0,0"/>
</Grid>

C# Code:

private string Id;
    private string ImgURL;
    public VideoDetails(string Id)
    {
        InitializeComponent();
        this.Id = Id;
        DetailsGenerator dg = new DetailsGenerator(Id);
        this.ImgURL = "Dynamic source";
        this.DataContext = BindImgURL;
        MessageBox.Show(BindImgURL);

    }
    public string BindImgURL
    {
        get { return ImgURL; }
        set
        { ImgURL = value; }
    }

Displaying Source correct! checked by MessageBox.But No image what i did wrong? I have tried removing "path=" but doesn't worked

Upvotes: 0

Views: 920

Answers (1)

dkozl
dkozl

Reputation: 33364

I imagine you either want to set DataContext to this

this.DataContext = this;

or change binding to current DataContext

<Image Source="{Binding}" .../>

at the moment Image will search for BindImgURL in current DataContext, which is set to BindImgURL and that is a string

Not related to your problem but I would also suggest you look into implementing INotifyPropertyChanged and raising PropertyChanged event for BindImgURL as, at the moment, after you set DataContext any change to BindImgURL won't be picked up by UI

Upvotes: 1

Related Questions