iko popo
iko popo

Reputation: 57

Switch in ListView doesn't fire

I am filling my settings in list view, however I wanted to leave it generic and just set class like this however when I switch the value it doesn't fire the command for my switch I have used this to add the command. It works outside of a list https://damienaicheh.github.io/xamarin/xamarin.forms/2019/05/17/how-to-add-command-to-a-switch-in-xamarin-forms-en.html

public class Settings
{
    public string Type { get; set; }
    public bool Value { get; set; }
    public Command command { get; set; }
}

then I am loading specific settings like this

public CustomSettingsViewModel()
{
    LoadSetting();
   
}

private void LoadSetting()
{
    foreach (var setting in _customSettings.ScanSettings)
    {
        var detail = new Settings()
        {
            Type = setting.Type,
            Value = setting.Value,
            command = new Command<Settings>((value) => ExecutePageCommand(value))
        };
        Setting.Add(detail);
    }
}

command

private void ExecutePageCommand(Settings settings)
{
    foreach (var result in _customSettings.ScanSettings.Where(d => d.Type == settings.Type))
    {
        var detail = new ScanSetting()
        {
            Value = settings.Value,
        };
    }
}
<Grid  BackgroundColor="{DynamicResource PageBackgroundColor}"  x:Name="grid" HorizontalOptions="FillAndExpand" ColumnSpacing="0">
  <!--Data-->
  <Label Grid.Row="0" Text ="{Binding Type}"   Style="{StaticResource SubLabelStyle}" HorizontalOptions="Start" Padding="10" VerticalOptions="End"   />
  <Switch Grid.Row="0"  IsToggled="{Binding Value}" HorizontalOptions="End" VerticalOptions="CenterAndExpand" >
    <Switch.Behaviors>
      <control:SwitchBehaviour Command="{Binding  command}" />
    </Switch.Behaviors>
  </Switch>
</Grid>    

However I am unsure how to change the value of the switch on the exact property so it would change only one value. Can you please advise?

Upvotes: 0

Views: 43

Answers (0)

Related Questions