How to set a control's visibility dynamically using a converter?

WilliamSmithWilliamSmith USMember
edited December 2016 in Xamarin.Forms

I have a set of controls that I want to be visible based on a property on the underlying model. This property is an Enum, and there are several UI configurations based on each value. When the page loads, everything works fine as expected. However, when the underlying model changes and RaisePropertyChanged() is called, the binding is not updating the visibility. Here's an example of my initial code:

IsVisible="{Binding EventItemViewModel.Event.EventStatusFk, 
                    Converter={StaticResource StatusVisibility}, 
                    ConverterParameter={x:Static entities:EventStatus.Scheduled}}"
private EventItemViewModel _eventItemViewModel;
public EventItemViewModel EventItemViewModel
{
    get { return _eventItemViewModel; }
    protected set
    {
        if (_eventItemViewModel == value) return;
        _eventItemViewModel = value;
        RaisePropertyChanged();
    }
}

void ChangeEventStatus(EventStatus change)
{
    EventItemViewModel temp = EventItemViewModel ;
    temp.Event.EventStatusFk = change;
    EventItemViewModel = temp;
}

In this case the underlying converter looks at the EventStatusFk and compares it to the enum value EventStatus.Scheduled and returns true if they match. Again, this works fine when the page first loads but never again if EventStatusFk changes.

I have also performed a test which simplifies this by creating a boolean property on the page model and binding IsVisible to that property:

IsVisible="{Binding visible}"
private bool _visible;
public bool visible {
    get { return _visible; }
    set {
        _visible = value;
        RaisePropertyChanged();
    }
}

When I change the underlying property and call RaisePropertyChanged(), the control's visibility updates as I expect it to. So I have a fallback to make this work, but it feels wrong to require a bool backer property for each control or control group.

Any suggestions?

Best Answers

Answers

  • WilliamSmithWilliamSmith USMember
    edited December 2016

    OK so although I appreciate the extra effort on your part you could probably have just pointed out that I missed the copy error without the CS 101 lesson in reference types. Just sayin. ;)

    In my mind I was trying to force the RaisePropertyChanged() method to fire in the setter since changing a nested property wasn't getting the job done. However, I didn't realize you could specify a property name in that method so I do greatly appreciate the example. Once I changed the code to update the property then call RaisePropertyChange("EventItemViewModel"), my original example began to work.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @WilliamSmith said:
    OK so although I appreciate the extra effort on your part you could probably have just pointed out that I missed the copy error without the CS 101 lesson in reference types. Just sayin. ;)

    In my mind I was trying to force the RaisePropertyChanged() method to fire in the setter since changing a nested property wasn't getting the job done. However, I didn't realize you could specify a property name in that method so I do greatly appreciate the example. Once I changed the code to update the property then call RaisePropertyChange("EventItemViewModel"), my original example began to work.

    Then I guess the CS101 lesson had value after all.

  • WilliamSmithWilliamSmith USMember
    edited December 2016

    Then I guess the CS101 lesson had value after all.

    Not really.

Sign In or Register to comment.