How do I turn on all switches with a button click using MVVM?

DylanWDylanW USMember ✭✭

Hello,

I am looking for some guidance on how I would go about creating a "Select All" feature in xamarin forms.

I have a bunch of switches that are being bound to a IsSelected property in the view model. I would like to be able to click a button and have all the switches in the list view turn on and off.

Thanks in advance,
Dylan

Answers

  • GeraldVersluisGeraldVersluis NLUniversity ✭✭✭✭

    It shouldn't be that hard.

    Just implement the INotifyPropertyChanged on your ViewModel, create a Command, call the Command with your button and in the command set all the objects to IsSelected = true or IsSelected = false

  • VM.0891VM.0891 DEMember

    I do a similar thing:
    View (ListView.ViewCell including Switch, Binding to ViewModel.bool-propery with are controling the model-bool, bindingmode: twoway) - ViewModel (incl. ViewModel of ViewCell, ItemSource of the ListView) - Model (incl. bool)

    btnSelectAll.clicked += (sender s, args e) => {
    foreach (var hSwitchViewModel in ListView_ViewModel.ViewCells)
    hSwitchViewModel.myBool = true;
    }

    ...something like this...

  • DylanWDylanW USMember ✭✭

    @GeraldVersluis Hi, It isn't bound to a property in the view model. IsSelected is a property apart of a object. So I have a loop that is setting all of the IsSelected object properties to true but the switches aren't being turned on. Does this make sense?

  • GeraldVersluisGeraldVersluis NLUniversity ✭✭✭✭

    I'm sorry, but it's not making a lot of sense.

    If it isn't bound to anything, how does this have anything to do with mvvm?
    Please post some code so I can understand better.

  • DylanWDylanW USMember ✭✭
    edited February 2016

    `

    `

    Then in the view model I have a Car model object that has a IsSelected a part of the model. That is what it is binding to.

  • DylanWDylanW USMember ✭✭
    edited February 2016

    @GeraldVersluis `<Button'
    x:Name="btnSelectAll"
    Text="{local:Translate SelectAll}"
    Clicked="OnSelectAllClicked"
    IsVisible="{Binding SelectAllButtonIsVisible}"
    IsEnabled="True"
    Grid.Column="2"
    MinimumWidthRequest="0" />

    '<Switch'
    IsToggled="{Binding IsSelected, Mode=TwoWay}"
    IsVisible="{Binding IsVisible}"
    Grid.Column="0" />`

    Then in the view model I have a Car model object that has a IsSelected a part of the model. That is what it is binding to.

  • DylanWDylanW USMember ✭✭

    @DylanW said:
    @GeraldVersluis `<Button'
    x:Name="btnSelectAll"
    Text="{local:Translate SelectAll}"
    Clicked="OnSelectAllClicked"
    IsVisible="{Binding SelectAllButtonIsVisible}"
    IsEnabled="True"
    Grid.Column="2"
    MinimumWidthRequest="0" />

    '<Switch'
    IsToggled="{Binding IsSelected, Mode=TwoWay}"
    IsVisible="{Binding IsVisible}"
    Grid.Column="0" />`

    Then in the view model I have a Car model object that has a IsSelected a part of the model. That is what it is binding to.

    The switch is in a list view if that determines what I should do.

  • GeraldVersluisGeraldVersluis NLUniversity ✭✭✭✭

    But if you are using data-binding then in your OnSelectAllClicked you should be able to loop through your Cars and set the IsSelected to true or false and the switches in the UI should change accordingly.

Sign In or Register to comment.