Nathiel Paulino Nathiel Paulino - 1 month ago 21
C# Question

How to handle image visibility using MVVM Xamarin Forms

I have a image which I was able to bind with my Image property on XAML,the problem is, when I pop up my View I want that image stays invisible, and then when o click my button appears again so far I got this in m ViewModel :

private string imageSource;

public string ImageSource
{
get { return imageSource; }
set { imageSource = value; Notify("ImageSource"); }
}
public bool State { get { return false; }}


private Image visibleImage;

public Image VisibleImage
{
get { return new Image {IsVisible = State,Source = ImageSource }; }
set { visibleImage = value; Notify("VisibleImage"); Notify("State"); }
}


In my
BindingContext
the property IsVisible is set as false, but doesn't work!

Answer

You could simplify your sample by avoiding to hold on to the Image reference in the view model at all. Modify your State flag like that:

public boolean State { get; set; }

In XAML declare the Image with its source and visibility binding:

<Image Source="{Binding ImageSource}" IsVisible="{Binding State}" />

Upon initialization of the view model set your State to false. The button click would then need to set the flag to true to make the image visible.