Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7018167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:00:34+00:00 2026-05-27T23:00:34+00:00

I have a WPF markup extension in charge of retrieving images by name, returning

  • 0

I have a WPF markup extension in charge of retrieving images by name, returning a BitmapImage object.

<Image Source="{my:ImageProvider ImageName=myImageName}"></Image>

Since retrieving an image is an operation that can possibly take a few seconds, I’d like to show a default image and display the requested image once it’s ready.

What I tried to do is something like this, but as this may change the BitmapImage object, it won’t update the UI (sample code):

BitmapImage img;
public override object ProvideValue(IServiceProvider serviceProvider)
{
    img = new BitmapImage(new Uri(@"D:\defaultImage.png", UriKind.Absolute));

    BackgroundWorker bw = new BackgroundWorker();
    bw.DoWork += new DoWorkEventHandler(bw_DoWork);
    bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
    bw.RunWorkerAsync();

    return img;
}

void bw_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
    System.Threading.Thread.Sleep(5000);
}

void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    img.UriSource = new Uri(@"D:\actualImage.png", UriKind.Absolute);
}

Is there a way I can update the UI to use the modified BitmapImage (something like INotifyPropertyChanged) or is there a different approach to achieve this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:00:35+00:00Added an answer on May 27, 2026 at 11:00 pm

    I ended up with two ways to do this. Both ways use a class that wraps the image and implements INotifyPropertyChanged:

    class ImageSourceWrapper : ObservableObject
    {
        private ImageSource _image;
        public ImageSource Image
        {
            get { return _image; }
            set
            {
                if (value != _image)
                {
                    _image = value;
    
                    RaiseOnPropertyChanged("Image");
                }
            }
        }
    
        public ImageSourceWrapper(ImageSource image)
        {
            Image = image;
        }
    }
    

    First Approach

    Once I have this, I can have my markup extension return an ImageSourceWrapper object and bind to it, like so

    <Image Source="{Binding Source={my:ImageProvider ImageName=myImageName}, Path=Image}" />
    

    I didn’t really like this way, since it’s pretty messy and involves having to know the ImageSourceWrapper class rather than working simply with ImageSource. Then I came up with the second approach.

    Second Approach

    In this approach I still use the ImageSourceWrapper class, but instead of having my markup extension return an ImageSourceWrapper object, I return a binding object which I set up to be bound to an ImageSourceWrapper object.

    The markup extension looks something like this:

    private ImageSourceWrapper _imageSourceWrapper;
    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        // Get the object and the property to be bound.
        IProvideValueTarget service = IProvideValueTarget)provider.GetService(typeof(IProvideValueTarget));
        DependencyObject targetObject = service.TargetObject as DependencyObject;
        DependencyProperty targetProperty = service.TargetProperty as DependencyProperty;
    
        // Set up the binding with the default image.
        _imageSourceWrapper = new ImageSourceWrapper(DefaultImage);
        Binding binding = new Binding("Image");
        binding.Source = _imageSourceWrapper;
        BindingOperations.SetBinding(targetObject, targetProperty, binding);
    
        // Retrieve the actual image asynchronously.
        GetImageAsync();
    
        return binding.ProvideValue(serviceProvider);
    }
    
    private void GetImageAsync()
    {
        // Get the image asynchronously.
    
        // Freeze the image so it could be accessed from all threads regardless
        // of which thread it was created on.
        newImage.Freeze();
    
        // Got the image - update the _imageSourceWrapper object.
        _imageSourceWrapper = newImage;
    }
    

    Then I can use it in XAML like this

    <Image Source="{my:ImageProvider ImageName=myImageName}" />
    

    This way the default image is displayed first, and once the requested image is retrieved, it will be displayed instead.

    Sorry if the code here isn’t entirely correct, I’m not near the code at the moment. Hopefully this is enough at the moment to express the main idea.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have WPF Span that is used as a source to a TextBlock. I
I have the following XAML markup in a WPF DataGrid: <DataGrid ItemsSource={Binding ResultList} Grid.ColumnSpan=4
I have been experimenting with WPF and rendering strict XAML markup in a web
I have this XAML markup... <Grid Name=ProductsGrid Width=500> <Grid.RowDefinitions> <RowDefinition Height=*/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition
I have WPF Application where I have One main form and other user controls
I have WPF Form which has many buttons with the same code. Appearance of
I have WPF app with 2 windows - whats the best way to syncronise
I have WPF C# application that communicate with a PLC (i.e. write/read Omron PLC's
I have a WPF application which connects to a remote database over internet and
I have a WPF application, where as part of the startup, I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.