-1

So i have an observable collection of buttons where i am trying to bind IsVisible to one of the buttons and the button displays even when i hardcode IsVisible to false.

View:

<c:MobileFocusBasePage.BottomRegionItems>
    <c:CollapsableButtonList ItemsSource="{Binding ActionItems}" HorizontalOptions="CenterAndExpand" WidthRequest="-1"/>
</c:MobileFocusBasePage.BottomRegionItems>

Model:

public ObservableCollection<View> ActionItems { get; set; } = new ObservableCollection<View>();

ActionItems.Add(new PaddedButton { BindingContext = NewWorkOrderButton, Text = ResString("Portal-WorkOrder"), Style = s, IsVisible = false});

Would there be a reason the IsVisible is being ignored in this case?

Thanks in advance for any help

Maxqueue
  • 2,194
  • 2
  • 23
  • 55

1 Answers1

0

I personally prefer to back the Public properties with a field.

private private ObservableCollection<View> _actionItems;

public ObservableCollection<View> ActionItems 
{
    get
    {
        return this._actionItems;
    }
    set
    {
        this._actionItems = value;        
    }
}

ObservableCollection provides change notification to the UI, when you add/remove/move an object in the collection. For more information, please see post.

The problem you are most likely encountering is, there is no change notification from your model. So when the IsVisibile property is updated in your viewmodel, how does the view know it changed? Generally agreed upon method of notifying the view is to use INotifyPropertyChanged interface.

I don't know the specific implementation of your view class, but here is an example.

public  class View : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string PropertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(PropertyName));
    }

    private bool _isVisible;
    public bool IsVisible
    {
        get
        {
            return this._isVisible;
        }
        set
        {
            this._isVisible = value;
            this.OnPropertyChanged(IsVisible) <--- The Magic! 
        }
    }
}

you have to bind the IsVisible property in your xaml and whenever this property is updated, the view will follow suit!

INotifyPropertyChanged interface is widely used and 90% of the code below is boilerplate stuff. You can put it in a base class or use a MVVM framework that provides this stuff. If you would like more information on how to implement INotifyPropertyChanged interface, please see post.

Neil
  • 629
  • 5
  • 14