I want a progressbar to be visible until a download is finished.
In order to do that, I bind the “Visibility” property with an attribute called “loadBarVisibility”.
XAML Code :
<ProgressBar Visibility="{Binding loadBarVisibility}" IsIndeterminate="True"/>
ViewModel Code :
public class MainViewModel : ViewModelBase
{
public static Visibility loadBarVisibility { get; set; }
private const String URL = "myurl";
public MainViewModel()
{
loadFlux();
if (IsInDesignMode)
{
// Code runs in Blend --> create design time data.
}
else
{
// Code runs "for real"
}
}
public static void loadFlux()
{
loadBarVisibility = Visibility.Visible;
WebClient client = new WebClient();
client.DownloadStringAsync(new Uri(URL));
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(test_downloadFinished);
}
public static void test_downloadFinished(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error == null)
{
loadBarVisibility = Visibility.Collapsed;
}
}
}
But the code above doesn’t work : the progressbar is still visible.
Otherwise, when I put “loadBarVisibility = Visibility.Collapsed;” just after “client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(test_downloadFinished);”, it works.
Is there a way to solve my problem ?
Thanks a lot.
Your ViewModel need to implement the interface INotifyPropertyChanged to notify you’re view that the property visiblilty was changed (http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx)