0

I am trying to change a image in the UI running with WPF on runtime by changing the source of the image. Unfortunately the image is not changing. I found examples of using a Bitmap image to change the Image source, but with ReactiveUI I am not able to change the UriSource for the BitmapImage.

XAML code:

  <Image x:Name="button_image0" DockPanel.Dock="Top" Source="someimage.jpg" Stretch="Uniform" />

view code:

this.OneWayBind(ViewModel, vm => vm.button_image0, v => v.button_image0).DisposeWith(d);

Viewmodel code:

[Reactive] public string button_image0 { get; set; }


//button pressed
button_image0 = "newimage.jpg";

I checked that the source of the image is changing, but the image displayed is not. Does someone know how to solve this with reactiveUI or knows a way around?

Solution If anyone is having the same problem, it's a syntax error of your code. When changing the source of an image using reactiveUI the image is reloaded. This question and answer confused me Reloading an image in wpf

Joni
  • 1
  • 1
  • Is it possible you're not doing the update on the UI thread? Use RxApp.MainThreadScheduler when you change your image. – Lee McPherson Dec 28 '20 at 23:23
  • How do I use the scheduler? I have tried `this.WhenAnyValue(x => x.button_image0).ObserveOn(RxApp.MainThreadScheduler);`, but the image is not reloading when the source is changed. – Joni Dec 29 '20 at 20:09
  • That's the way to use it. If a method has a parameter for the scheduler, use it there instead. But the answer listed is probably your problem... I missed that. – Lee McPherson Jan 01 '21 at 22:38

1 Answers1

2

When binding you should include the control property as part of the expression. For example

ContentControl.Content

SizableControl.Width

Image.Source

TextBlock.Text

So if you bind to the "Source" property on the Image on the view. You also appear to have your notation of v and vm the wrong way around which might be adding to the confusion. The viewmodel is the 2nd expression, the view is the 3rd.

this.OneWayBind(ViewModel, vm => vm.button_image0, vw => vw.button_image0.Source).DisposeWith(d);

DPVreony
  • 81
  • 3