goldsmit409
goldsmit409

Reputation: 478

how to clear image control from local path

i have a image control in wpf like this

<Image x:Name="Img" Source="{Binding IsAsync=True}" />

i set the image by fetching from a url like this

  Img.DataContext = ImageUrl;

it shows fine and when i want to clear it i just use

 Img.DataContext=null;

for the same control i have a browse button as well to select the image from local path like this

 BitmapImage image = new BitmapImage(new Uri(path));
 Img.Source=image;

now i want to clear that as well so i do

 Img.Source=null;

after that the control wont show the image from url only local images can be opened

Edit: probably i need to set the binding again after making source to null, not sure how to do that

Upvotes: 2

Views: 2448

Answers (2)

anagha
anagha

Reputation: 63

To clear image control, clear image url.

e.g imgControlName.ImageUrl = "";

Upvotes: 0

BradleyDotNET
BradleyDotNET

Reputation: 61349

You are abusing bindings horribly. Please stop.

<Image x:Name="Img" Source="{Binding IsAsync=True}" />

Says "Bind to the data context", which isn't all that great. Bind to a property of your view model, something like:

<Image x:Name="Img" Source="{Binding Path=ImageLocation, IsAsync=True}" />

And then only ever change the image using ImageLocation. At the very least, only set it via the DataContext.

Once you set the source to a binding, you should never be changing it via code-behind. Period. Do that, and your problem will "magically" disappear, as you are doing it the right way now.

Upvotes: 1

Related Questions