I have a bunch of labels that I want to display for people. For example:

Name <value>
Age <value>
Cell <value>
Home# <value>

etc., but I can't guarantee that all of those fields have data in them. To compensate, I use an IValueConverter to look at a string, determine if it's null or whitespace, and then bind the IsVisible property like so:


<Label Style="{StaticResource gridLabel}" Text="{i18n:Translate CellPhone}"  IsVisible="{Binding CellPhone, Converter={StaticResource StringNull}}" Grid.Row="11" Grid.Column="0" VerticalOptions="Center"/>
<Label Style="{StaticResource gridValue}" Text="{Binding CellPhone}" IsVisible="{Binding CellPhone, Converter={StaticResource StringNull}}" Grid.Row="11" Grid.Column="1" TextColor="Blue" Margin="0,10,0,10" VerticalOptions="Center" />


    public class StringNullOrEmptyBoolConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {            
            if (value != null)
            {
                string s = value.ToString().Trim();
                return !string.IsNullOrWhiteSpace(s);
            }
            return false;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

My ViewModel only exposes specific fields. All of my properties are things like this:


public string CellPhone => _details.CellPhone;

So when I switch people and try to refresh the page, I tell the ViewModel to go through its properties and fire off OnPropertyChanged for each:


private async Task GetData()
{
    _details = await MyWebService.GetPersonDetails();

    foreach (var property in GetType().GetProperties())
        OnPropertyChanged(nameof(property));
}

private void OnPropertyChanged(string propertyName)
{
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

I can watch in the debugger and see all of those PropertyChanged events happen. But when I switch people, the labels that are hidden or not hidden remain that way. Some of those labels that were displayed no longer have data so I want to hide them, and the labels that were not displayed but have data are still invisible. I can check this for myself by taking out the Converter in the label. The data updates but the IValueConverter does not re-fire. I've made sure of this by setting a breakpoint. When the page is created, the first person fires them, but when I switch people, they never fire again.

What can I do to re-run all the IValueConverters? I'm totally stuck.

0 Answers