I am trying to create a page on which a progress bar will be displayed until the data has been successfully downloaded from the server.
To do this, I use a generic data loader that simply populates the model properties and sets the IsLoading property to true and / or false
View models are as follows:
public class GenericPageModel: GenericModel { private bool _isLoading; public bool IsLoading { get { return _isLoading; } set { _isLoading = value; OnPropertyChanged("IsLoading"); } } } public class GenericModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged(string propName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propName)); } } }
The GenericPageModel is used on the XAML page as a model, and the IsLoading property is used below:
<Grid> <Grid.Resources> <BooleanToVisibilityConverter x:Key="boolToVis"/> </Grid.Resources> <ProgressBar Height="25" Margin="5" VerticalAlignment="Center" HorizontalAlignment="Stretch" Visibility="{Binding IsLoading, Converter={StaticResource boolToVis}}" IsIndeterminate="True" /> </Grid>
General data loader:
... // Model that it calling this public object Model { get; set; } private string _loadingProperty; ... void _bw_DoWork(object sender, DoWorkEventArgs e) { // Set the is loading property if (null != _loadingProperty) { //((Model as GenericModel).Owner as GenericPageModel).IsLoading = true; // Works!! Model.GetType().GetProperty(_loadingProperty).SetValue(Model, true, null); // Doesn't work } }
If I explicitly apply Model to GenericPageModel and set IsLoading to true, everything works fine (see commented line)
If I use reflection to set the property value, then the set IsLoading parameter is set correctly, the OnPropertyChanged method is called ok, but the user interface does not update
Is there anything extra that needs to be done when setting up property reflection? I suppose that events are not properly raised, but I cannot figure out what to do.
It was decided that before calling the bootloader an additional model was added, the line should say:
object Owner = Model.GetType().GetProperty("Owner").GetValue(Model, null); Model.GetType().GetProperty(_loadingProperty).SetValue(Owner, true, null);