When i click the event on outside of listview switch button or inside of listview switch button, it will...











up vote
0
down vote

favorite












I am using xamarin forms listview.
I am using switch control on both listview and outside of listview:
When i click the event on outside of listview switch button or inside of listview switch button, it will affect bothin event class.
How to avoid these issue.



My code for xaml file is below:



  <Label Margin="0,0,10,0" Text="Select All" TextColor="Black" FontSize="Medium" HorizontalOptions="End" VerticalOptions="Center"></Label>
<Switch Margin="0,0,10,0" x:Name="SelectToggled" HorizontalOptions="End" VerticalOptions="Center" Toggled="Switch_Toggled" ></Switch>

<ListView x:Name="LocationListView" HasUnevenRows="True" SeparatorVisibility="None" VerticalOptions="FillAndExpand">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid Margin="0,0,0,0" BackgroundColor="{Binding backgroundColor}" HeightRequest="47" RowSpacing="0" HorizontalOptions="FillAndExpand" Padding="20,0,0,0">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80*"/>
<ColumnDefinition Width="20*"/>
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" Text="{Binding CountryName}" HorizontalOptions="Start" VerticalOptions="Center" TextColor="Black" FontFamily="Roboto" FontSize="Medium"></Label>
<Switch Grid.Row="0" Grid.Column="1" HorizontalOptions="CenterAndExpand" Toggled="SwitchList_Toggled" VerticalOptions="CenterAndExpand" IsToggled="{Binding IsToggle,Mode=TwoWay}"></Switch>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>


My code for xaml.cs is:



 public MarketLanding()
{
InitializeComponent();
ObservableCollection<Country> countrylist = new ObservableCollection<Country>();
countrylist.Add(new Country { CountryName = "India" });
countrylist.Add(new Country { CountryName = "America" });
countrylist.Add(new Country { CountryName = "England" });

for (int i = 0; i < countrylist.Count; i++)
{
if (i % 2 == 0)
{
countrylist[i].backgroundColor = Color.LightGray;
}
else
{
countrylist[i].backgroundColor = Color.White;
}
}
LocationListView.ItemsSource = countrylist;
}

private void Switch_Toggled(object sender, ToggledEventArgs e)
{
}

private void SwitchList_Toggled(object sender, ToggledEventArgs e)
{
}


Anyone please help me to sort out this issue.










share|improve this question


























    up vote
    0
    down vote

    favorite












    I am using xamarin forms listview.
    I am using switch control on both listview and outside of listview:
    When i click the event on outside of listview switch button or inside of listview switch button, it will affect bothin event class.
    How to avoid these issue.



    My code for xaml file is below:



      <Label Margin="0,0,10,0" Text="Select All" TextColor="Black" FontSize="Medium" HorizontalOptions="End" VerticalOptions="Center"></Label>
    <Switch Margin="0,0,10,0" x:Name="SelectToggled" HorizontalOptions="End" VerticalOptions="Center" Toggled="Switch_Toggled" ></Switch>

    <ListView x:Name="LocationListView" HasUnevenRows="True" SeparatorVisibility="None" VerticalOptions="FillAndExpand">
    <ListView.ItemTemplate>
    <DataTemplate>
    <ViewCell>
    <Grid Margin="0,0,0,0" BackgroundColor="{Binding backgroundColor}" HeightRequest="47" RowSpacing="0" HorizontalOptions="FillAndExpand" Padding="20,0,0,0">
    <Grid.RowDefinitions>
    <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
    <ColumnDefinition Width="80*"/>
    <ColumnDefinition Width="20*"/>
    </Grid.ColumnDefinitions>
    <Label Grid.Row="0" Grid.Column="0" Text="{Binding CountryName}" HorizontalOptions="Start" VerticalOptions="Center" TextColor="Black" FontFamily="Roboto" FontSize="Medium"></Label>
    <Switch Grid.Row="0" Grid.Column="1" HorizontalOptions="CenterAndExpand" Toggled="SwitchList_Toggled" VerticalOptions="CenterAndExpand" IsToggled="{Binding IsToggle,Mode=TwoWay}"></Switch>
    </Grid>
    </ViewCell>
    </DataTemplate>
    </ListView.ItemTemplate>
    </ListView>


    My code for xaml.cs is:



     public MarketLanding()
    {
    InitializeComponent();
    ObservableCollection<Country> countrylist = new ObservableCollection<Country>();
    countrylist.Add(new Country { CountryName = "India" });
    countrylist.Add(new Country { CountryName = "America" });
    countrylist.Add(new Country { CountryName = "England" });

    for (int i = 0; i < countrylist.Count; i++)
    {
    if (i % 2 == 0)
    {
    countrylist[i].backgroundColor = Color.LightGray;
    }
    else
    {
    countrylist[i].backgroundColor = Color.White;
    }
    }
    LocationListView.ItemsSource = countrylist;
    }

    private void Switch_Toggled(object sender, ToggledEventArgs e)
    {
    }

    private void SwitchList_Toggled(object sender, ToggledEventArgs e)
    {
    }


    Anyone please help me to sort out this issue.










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I am using xamarin forms listview.
      I am using switch control on both listview and outside of listview:
      When i click the event on outside of listview switch button or inside of listview switch button, it will affect bothin event class.
      How to avoid these issue.



      My code for xaml file is below:



        <Label Margin="0,0,10,0" Text="Select All" TextColor="Black" FontSize="Medium" HorizontalOptions="End" VerticalOptions="Center"></Label>
      <Switch Margin="0,0,10,0" x:Name="SelectToggled" HorizontalOptions="End" VerticalOptions="Center" Toggled="Switch_Toggled" ></Switch>

      <ListView x:Name="LocationListView" HasUnevenRows="True" SeparatorVisibility="None" VerticalOptions="FillAndExpand">
      <ListView.ItemTemplate>
      <DataTemplate>
      <ViewCell>
      <Grid Margin="0,0,0,0" BackgroundColor="{Binding backgroundColor}" HeightRequest="47" RowSpacing="0" HorizontalOptions="FillAndExpand" Padding="20,0,0,0">
      <Grid.RowDefinitions>
      <RowDefinition Height="*"/>
      </Grid.RowDefinitions>
      <Grid.ColumnDefinitions>
      <ColumnDefinition Width="80*"/>
      <ColumnDefinition Width="20*"/>
      </Grid.ColumnDefinitions>
      <Label Grid.Row="0" Grid.Column="0" Text="{Binding CountryName}" HorizontalOptions="Start" VerticalOptions="Center" TextColor="Black" FontFamily="Roboto" FontSize="Medium"></Label>
      <Switch Grid.Row="0" Grid.Column="1" HorizontalOptions="CenterAndExpand" Toggled="SwitchList_Toggled" VerticalOptions="CenterAndExpand" IsToggled="{Binding IsToggle,Mode=TwoWay}"></Switch>
      </Grid>
      </ViewCell>
      </DataTemplate>
      </ListView.ItemTemplate>
      </ListView>


      My code for xaml.cs is:



       public MarketLanding()
      {
      InitializeComponent();
      ObservableCollection<Country> countrylist = new ObservableCollection<Country>();
      countrylist.Add(new Country { CountryName = "India" });
      countrylist.Add(new Country { CountryName = "America" });
      countrylist.Add(new Country { CountryName = "England" });

      for (int i = 0; i < countrylist.Count; i++)
      {
      if (i % 2 == 0)
      {
      countrylist[i].backgroundColor = Color.LightGray;
      }
      else
      {
      countrylist[i].backgroundColor = Color.White;
      }
      }
      LocationListView.ItemsSource = countrylist;
      }

      private void Switch_Toggled(object sender, ToggledEventArgs e)
      {
      }

      private void SwitchList_Toggled(object sender, ToggledEventArgs e)
      {
      }


      Anyone please help me to sort out this issue.










      share|improve this question













      I am using xamarin forms listview.
      I am using switch control on both listview and outside of listview:
      When i click the event on outside of listview switch button or inside of listview switch button, it will affect bothin event class.
      How to avoid these issue.



      My code for xaml file is below:



        <Label Margin="0,0,10,0" Text="Select All" TextColor="Black" FontSize="Medium" HorizontalOptions="End" VerticalOptions="Center"></Label>
      <Switch Margin="0,0,10,0" x:Name="SelectToggled" HorizontalOptions="End" VerticalOptions="Center" Toggled="Switch_Toggled" ></Switch>

      <ListView x:Name="LocationListView" HasUnevenRows="True" SeparatorVisibility="None" VerticalOptions="FillAndExpand">
      <ListView.ItemTemplate>
      <DataTemplate>
      <ViewCell>
      <Grid Margin="0,0,0,0" BackgroundColor="{Binding backgroundColor}" HeightRequest="47" RowSpacing="0" HorizontalOptions="FillAndExpand" Padding="20,0,0,0">
      <Grid.RowDefinitions>
      <RowDefinition Height="*"/>
      </Grid.RowDefinitions>
      <Grid.ColumnDefinitions>
      <ColumnDefinition Width="80*"/>
      <ColumnDefinition Width="20*"/>
      </Grid.ColumnDefinitions>
      <Label Grid.Row="0" Grid.Column="0" Text="{Binding CountryName}" HorizontalOptions="Start" VerticalOptions="Center" TextColor="Black" FontFamily="Roboto" FontSize="Medium"></Label>
      <Switch Grid.Row="0" Grid.Column="1" HorizontalOptions="CenterAndExpand" Toggled="SwitchList_Toggled" VerticalOptions="CenterAndExpand" IsToggled="{Binding IsToggle,Mode=TwoWay}"></Switch>
      </Grid>
      </ViewCell>
      </DataTemplate>
      </ListView.ItemTemplate>
      </ListView>


      My code for xaml.cs is:



       public MarketLanding()
      {
      InitializeComponent();
      ObservableCollection<Country> countrylist = new ObservableCollection<Country>();
      countrylist.Add(new Country { CountryName = "India" });
      countrylist.Add(new Country { CountryName = "America" });
      countrylist.Add(new Country { CountryName = "England" });

      for (int i = 0; i < countrylist.Count; i++)
      {
      if (i % 2 == 0)
      {
      countrylist[i].backgroundColor = Color.LightGray;
      }
      else
      {
      countrylist[i].backgroundColor = Color.White;
      }
      }
      LocationListView.ItemsSource = countrylist;
      }

      private void Switch_Toggled(object sender, ToggledEventArgs e)
      {
      }

      private void SwitchList_Toggled(object sender, ToggledEventArgs e)
      {
      }


      Anyone please help me to sort out this issue.







      xamarin.forms xamarin.forms.listview






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 7 at 12:21









      Arun manthiram

      608




      608
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          You can foreach through CountryList, I do one sample that you can take a look:



          <?xml version="1.0" encoding="utf-8" ?>
          <ContentPage
          x:Class="Test1.MainPage"
          xmlns="http://xamarin.com/schemas/2014/forms"
          xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
          xmlns:local="clr-namespace:Test1">

          <StackLayout>
          <Label
          Margin="0,0,10,0"
          FontSize="Medium"
          HorizontalOptions="End"
          Text="Select All"
          TextColor="White"
          VerticalOptions="Center" />
          <Switch
          x:Name="SelectToggled"
          Margin="0,0,10,0"
          BackgroundColor="BlueViolet"
          HorizontalOptions="End"
          Toggled="Switch_Toggled"
          VerticalOptions="Center" />

          <ListView
          x:Name="LocationListView"
          BackgroundColor="AliceBlue"
          HasUnevenRows="True"
          SeparatorVisibility="None"
          VerticalOptions="FillAndExpand">
          <ListView.ItemTemplate>
          <DataTemplate>
          <ViewCell>
          <Grid
          Margin="0,0,0,0"
          Padding="20,0,0,0"
          BackgroundColor="{Binding backgroundColor}"
          HeightRequest="47"
          HorizontalOptions="FillAndExpand"
          RowSpacing="0">
          <Grid.RowDefinitions>
          <RowDefinition Height="*" />
          </Grid.RowDefinitions>
          <Grid.ColumnDefinitions>
          <ColumnDefinition Width="80*" />
          <ColumnDefinition Width="20*" />
          </Grid.ColumnDefinitions>
          <Label
          Grid.Row="0"
          Grid.Column="0"
          FontFamily="Roboto"
          FontSize="Medium"
          HorizontalOptions="Start"
          Text="{Binding CountryName}"
          TextColor="Black"
          VerticalOptions="Center" />
          <Switch
          x:Name="switch1"
          Grid.Row="0"
          Grid.Column="1"
          HorizontalOptions="CenterAndExpand"
          IsToggled="{Binding IsToggle, Mode=TwoWay}"
          Toggled="SwitchList_Toggled"
          VerticalOptions="CenterAndExpand" />
          </Grid>
          </ViewCell>
          </DataTemplate>
          </ListView.ItemTemplate>
          </ListView>
          </StackLayout>




          public ObservableCollection<Country> countrylist { get; set; }
          public MainPage()
          {
          InitializeComponent();

          countrylist = new ObservableCollection<Country>();
          countrylist.Add(new Country { CountryName = "India",IsToggle=false });
          countrylist.Add(new Country { CountryName = "America" ,IsToggle=true});
          countrylist.Add(new Country { CountryName = "England" ,IsToggle=false});

          for (int i = 0; i < countrylist.Count; i++)
          {
          if (i % 2 == 0)
          {
          countrylist[i].backgroundColor = Color.LightGray;
          }
          else
          {
          countrylist[i].backgroundColor = Color.White;
          }
          }
          LocationListView.ItemsSource = countrylist;
          }

          private void Switch_Toggled(object sender, ToggledEventArgs e)
          {
          Console.WriteLine(" outside switch clicked");
          if(SelectToggled.IsToggled)
          {
          for (Int32 i = 0; i < countrylist.Count; i++)
          {
          countrylist[i].IsToggle = true;
          }
          }
          else
          {
          for (Int32 i = 0; i < countrylist.Count; i++)
          {
          countrylist[i].IsToggle = false;
          }
          }
          }

          private void SwitchList_Toggled(object sender, ToggledEventArgs e)
          {
          Console.WriteLine(" inside switch clicked");
          }





          public class Country:INotifyPropertyChanged
          {
          private string _CountryName;
          public string CountryName
          {
          get { return _CountryName; }
          set
          {
          _CountryName = value;
          RaisePropertyChanged("CountryName");
          }
          }
          private Color _backgroundColor;
          public Color backgroundColor
          {
          get { return _backgroundColor; }
          set
          {
          _backgroundColor = value;
          RaisePropertyChanged("backgroundColor");
          }
          }

          private bool _IsToggle;
          public bool IsToggle
          {
          get { return _IsToggle; }
          set
          {
          _IsToggle = value;
          RaisePropertyChanged("IsToggle");
          }
          }

          public event PropertyChangedEventHandler PropertyChanged;

          public void RaisePropertyChanged(string propertyName)
          {
          PropertyChangedEventHandler handler = PropertyChanged;
          if(handler!=null)
          {
          handler(this,new PropertyChangedEventArgs(propertyName));
          }
          }
          }





          share|improve this answer





















          • Hi Cherry, Thank your reply. But I need the following...
            – Arun manthiram
            Nov 9 at 13:39












          • When i Switched On the "Outside switch control", all "inside switch controls" should be switched On. When I Switched Off the "Outside switch control", all "inside switch controls" should be switched Off. When I switched Off/On any switch control in "inside switch Control",then the "outside switch control" should be automatically switched Off/On.
            – Arun manthiram
            Nov 9 at 13:39










          • @Arun, For When I switched Off/On any switch control in "inside switch Control",then the "outside switch control" should be automatically switched Off/On, I think it is not unreasonable, because When you switched Off/On any switch control in "inside switch Control",then the "outside switch control" should be automatically switched Off/On, then all "inside switch controls" should be switched On. . It is meaningless.
            – Cherry Bu
            Nov 12 at 7:21













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53189391%2fwhen-i-click-the-event-on-outside-of-listview-switch-button-or-inside-of-listvie%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote













          You can foreach through CountryList, I do one sample that you can take a look:



          <?xml version="1.0" encoding="utf-8" ?>
          <ContentPage
          x:Class="Test1.MainPage"
          xmlns="http://xamarin.com/schemas/2014/forms"
          xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
          xmlns:local="clr-namespace:Test1">

          <StackLayout>
          <Label
          Margin="0,0,10,0"
          FontSize="Medium"
          HorizontalOptions="End"
          Text="Select All"
          TextColor="White"
          VerticalOptions="Center" />
          <Switch
          x:Name="SelectToggled"
          Margin="0,0,10,0"
          BackgroundColor="BlueViolet"
          HorizontalOptions="End"
          Toggled="Switch_Toggled"
          VerticalOptions="Center" />

          <ListView
          x:Name="LocationListView"
          BackgroundColor="AliceBlue"
          HasUnevenRows="True"
          SeparatorVisibility="None"
          VerticalOptions="FillAndExpand">
          <ListView.ItemTemplate>
          <DataTemplate>
          <ViewCell>
          <Grid
          Margin="0,0,0,0"
          Padding="20,0,0,0"
          BackgroundColor="{Binding backgroundColor}"
          HeightRequest="47"
          HorizontalOptions="FillAndExpand"
          RowSpacing="0">
          <Grid.RowDefinitions>
          <RowDefinition Height="*" />
          </Grid.RowDefinitions>
          <Grid.ColumnDefinitions>
          <ColumnDefinition Width="80*" />
          <ColumnDefinition Width="20*" />
          </Grid.ColumnDefinitions>
          <Label
          Grid.Row="0"
          Grid.Column="0"
          FontFamily="Roboto"
          FontSize="Medium"
          HorizontalOptions="Start"
          Text="{Binding CountryName}"
          TextColor="Black"
          VerticalOptions="Center" />
          <Switch
          x:Name="switch1"
          Grid.Row="0"
          Grid.Column="1"
          HorizontalOptions="CenterAndExpand"
          IsToggled="{Binding IsToggle, Mode=TwoWay}"
          Toggled="SwitchList_Toggled"
          VerticalOptions="CenterAndExpand" />
          </Grid>
          </ViewCell>
          </DataTemplate>
          </ListView.ItemTemplate>
          </ListView>
          </StackLayout>




          public ObservableCollection<Country> countrylist { get; set; }
          public MainPage()
          {
          InitializeComponent();

          countrylist = new ObservableCollection<Country>();
          countrylist.Add(new Country { CountryName = "India",IsToggle=false });
          countrylist.Add(new Country { CountryName = "America" ,IsToggle=true});
          countrylist.Add(new Country { CountryName = "England" ,IsToggle=false});

          for (int i = 0; i < countrylist.Count; i++)
          {
          if (i % 2 == 0)
          {
          countrylist[i].backgroundColor = Color.LightGray;
          }
          else
          {
          countrylist[i].backgroundColor = Color.White;
          }
          }
          LocationListView.ItemsSource = countrylist;
          }

          private void Switch_Toggled(object sender, ToggledEventArgs e)
          {
          Console.WriteLine(" outside switch clicked");
          if(SelectToggled.IsToggled)
          {
          for (Int32 i = 0; i < countrylist.Count; i++)
          {
          countrylist[i].IsToggle = true;
          }
          }
          else
          {
          for (Int32 i = 0; i < countrylist.Count; i++)
          {
          countrylist[i].IsToggle = false;
          }
          }
          }

          private void SwitchList_Toggled(object sender, ToggledEventArgs e)
          {
          Console.WriteLine(" inside switch clicked");
          }





          public class Country:INotifyPropertyChanged
          {
          private string _CountryName;
          public string CountryName
          {
          get { return _CountryName; }
          set
          {
          _CountryName = value;
          RaisePropertyChanged("CountryName");
          }
          }
          private Color _backgroundColor;
          public Color backgroundColor
          {
          get { return _backgroundColor; }
          set
          {
          _backgroundColor = value;
          RaisePropertyChanged("backgroundColor");
          }
          }

          private bool _IsToggle;
          public bool IsToggle
          {
          get { return _IsToggle; }
          set
          {
          _IsToggle = value;
          RaisePropertyChanged("IsToggle");
          }
          }

          public event PropertyChangedEventHandler PropertyChanged;

          public void RaisePropertyChanged(string propertyName)
          {
          PropertyChangedEventHandler handler = PropertyChanged;
          if(handler!=null)
          {
          handler(this,new PropertyChangedEventArgs(propertyName));
          }
          }
          }





          share|improve this answer





















          • Hi Cherry, Thank your reply. But I need the following...
            – Arun manthiram
            Nov 9 at 13:39












          • When i Switched On the "Outside switch control", all "inside switch controls" should be switched On. When I Switched Off the "Outside switch control", all "inside switch controls" should be switched Off. When I switched Off/On any switch control in "inside switch Control",then the "outside switch control" should be automatically switched Off/On.
            – Arun manthiram
            Nov 9 at 13:39










          • @Arun, For When I switched Off/On any switch control in "inside switch Control",then the "outside switch control" should be automatically switched Off/On, I think it is not unreasonable, because When you switched Off/On any switch control in "inside switch Control",then the "outside switch control" should be automatically switched Off/On, then all "inside switch controls" should be switched On. . It is meaningless.
            – Cherry Bu
            Nov 12 at 7:21

















          up vote
          0
          down vote













          You can foreach through CountryList, I do one sample that you can take a look:



          <?xml version="1.0" encoding="utf-8" ?>
          <ContentPage
          x:Class="Test1.MainPage"
          xmlns="http://xamarin.com/schemas/2014/forms"
          xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
          xmlns:local="clr-namespace:Test1">

          <StackLayout>
          <Label
          Margin="0,0,10,0"
          FontSize="Medium"
          HorizontalOptions="End"
          Text="Select All"
          TextColor="White"
          VerticalOptions="Center" />
          <Switch
          x:Name="SelectToggled"
          Margin="0,0,10,0"
          BackgroundColor="BlueViolet"
          HorizontalOptions="End"
          Toggled="Switch_Toggled"
          VerticalOptions="Center" />

          <ListView
          x:Name="LocationListView"
          BackgroundColor="AliceBlue"
          HasUnevenRows="True"
          SeparatorVisibility="None"
          VerticalOptions="FillAndExpand">
          <ListView.ItemTemplate>
          <DataTemplate>
          <ViewCell>
          <Grid
          Margin="0,0,0,0"
          Padding="20,0,0,0"
          BackgroundColor="{Binding backgroundColor}"
          HeightRequest="47"
          HorizontalOptions="FillAndExpand"
          RowSpacing="0">
          <Grid.RowDefinitions>
          <RowDefinition Height="*" />
          </Grid.RowDefinitions>
          <Grid.ColumnDefinitions>
          <ColumnDefinition Width="80*" />
          <ColumnDefinition Width="20*" />
          </Grid.ColumnDefinitions>
          <Label
          Grid.Row="0"
          Grid.Column="0"
          FontFamily="Roboto"
          FontSize="Medium"
          HorizontalOptions="Start"
          Text="{Binding CountryName}"
          TextColor="Black"
          VerticalOptions="Center" />
          <Switch
          x:Name="switch1"
          Grid.Row="0"
          Grid.Column="1"
          HorizontalOptions="CenterAndExpand"
          IsToggled="{Binding IsToggle, Mode=TwoWay}"
          Toggled="SwitchList_Toggled"
          VerticalOptions="CenterAndExpand" />
          </Grid>
          </ViewCell>
          </DataTemplate>
          </ListView.ItemTemplate>
          </ListView>
          </StackLayout>




          public ObservableCollection<Country> countrylist { get; set; }
          public MainPage()
          {
          InitializeComponent();

          countrylist = new ObservableCollection<Country>();
          countrylist.Add(new Country { CountryName = "India",IsToggle=false });
          countrylist.Add(new Country { CountryName = "America" ,IsToggle=true});
          countrylist.Add(new Country { CountryName = "England" ,IsToggle=false});

          for (int i = 0; i < countrylist.Count; i++)
          {
          if (i % 2 == 0)
          {
          countrylist[i].backgroundColor = Color.LightGray;
          }
          else
          {
          countrylist[i].backgroundColor = Color.White;
          }
          }
          LocationListView.ItemsSource = countrylist;
          }

          private void Switch_Toggled(object sender, ToggledEventArgs e)
          {
          Console.WriteLine(" outside switch clicked");
          if(SelectToggled.IsToggled)
          {
          for (Int32 i = 0; i < countrylist.Count; i++)
          {
          countrylist[i].IsToggle = true;
          }
          }
          else
          {
          for (Int32 i = 0; i < countrylist.Count; i++)
          {
          countrylist[i].IsToggle = false;
          }
          }
          }

          private void SwitchList_Toggled(object sender, ToggledEventArgs e)
          {
          Console.WriteLine(" inside switch clicked");
          }





          public class Country:INotifyPropertyChanged
          {
          private string _CountryName;
          public string CountryName
          {
          get { return _CountryName; }
          set
          {
          _CountryName = value;
          RaisePropertyChanged("CountryName");
          }
          }
          private Color _backgroundColor;
          public Color backgroundColor
          {
          get { return _backgroundColor; }
          set
          {
          _backgroundColor = value;
          RaisePropertyChanged("backgroundColor");
          }
          }

          private bool _IsToggle;
          public bool IsToggle
          {
          get { return _IsToggle; }
          set
          {
          _IsToggle = value;
          RaisePropertyChanged("IsToggle");
          }
          }

          public event PropertyChangedEventHandler PropertyChanged;

          public void RaisePropertyChanged(string propertyName)
          {
          PropertyChangedEventHandler handler = PropertyChanged;
          if(handler!=null)
          {
          handler(this,new PropertyChangedEventArgs(propertyName));
          }
          }
          }





          share|improve this answer





















          • Hi Cherry, Thank your reply. But I need the following...
            – Arun manthiram
            Nov 9 at 13:39












          • When i Switched On the "Outside switch control", all "inside switch controls" should be switched On. When I Switched Off the "Outside switch control", all "inside switch controls" should be switched Off. When I switched Off/On any switch control in "inside switch Control",then the "outside switch control" should be automatically switched Off/On.
            – Arun manthiram
            Nov 9 at 13:39










          • @Arun, For When I switched Off/On any switch control in "inside switch Control",then the "outside switch control" should be automatically switched Off/On, I think it is not unreasonable, because When you switched Off/On any switch control in "inside switch Control",then the "outside switch control" should be automatically switched Off/On, then all "inside switch controls" should be switched On. . It is meaningless.
            – Cherry Bu
            Nov 12 at 7:21















          up vote
          0
          down vote










          up vote
          0
          down vote









          You can foreach through CountryList, I do one sample that you can take a look:



          <?xml version="1.0" encoding="utf-8" ?>
          <ContentPage
          x:Class="Test1.MainPage"
          xmlns="http://xamarin.com/schemas/2014/forms"
          xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
          xmlns:local="clr-namespace:Test1">

          <StackLayout>
          <Label
          Margin="0,0,10,0"
          FontSize="Medium"
          HorizontalOptions="End"
          Text="Select All"
          TextColor="White"
          VerticalOptions="Center" />
          <Switch
          x:Name="SelectToggled"
          Margin="0,0,10,0"
          BackgroundColor="BlueViolet"
          HorizontalOptions="End"
          Toggled="Switch_Toggled"
          VerticalOptions="Center" />

          <ListView
          x:Name="LocationListView"
          BackgroundColor="AliceBlue"
          HasUnevenRows="True"
          SeparatorVisibility="None"
          VerticalOptions="FillAndExpand">
          <ListView.ItemTemplate>
          <DataTemplate>
          <ViewCell>
          <Grid
          Margin="0,0,0,0"
          Padding="20,0,0,0"
          BackgroundColor="{Binding backgroundColor}"
          HeightRequest="47"
          HorizontalOptions="FillAndExpand"
          RowSpacing="0">
          <Grid.RowDefinitions>
          <RowDefinition Height="*" />
          </Grid.RowDefinitions>
          <Grid.ColumnDefinitions>
          <ColumnDefinition Width="80*" />
          <ColumnDefinition Width="20*" />
          </Grid.ColumnDefinitions>
          <Label
          Grid.Row="0"
          Grid.Column="0"
          FontFamily="Roboto"
          FontSize="Medium"
          HorizontalOptions="Start"
          Text="{Binding CountryName}"
          TextColor="Black"
          VerticalOptions="Center" />
          <Switch
          x:Name="switch1"
          Grid.Row="0"
          Grid.Column="1"
          HorizontalOptions="CenterAndExpand"
          IsToggled="{Binding IsToggle, Mode=TwoWay}"
          Toggled="SwitchList_Toggled"
          VerticalOptions="CenterAndExpand" />
          </Grid>
          </ViewCell>
          </DataTemplate>
          </ListView.ItemTemplate>
          </ListView>
          </StackLayout>




          public ObservableCollection<Country> countrylist { get; set; }
          public MainPage()
          {
          InitializeComponent();

          countrylist = new ObservableCollection<Country>();
          countrylist.Add(new Country { CountryName = "India",IsToggle=false });
          countrylist.Add(new Country { CountryName = "America" ,IsToggle=true});
          countrylist.Add(new Country { CountryName = "England" ,IsToggle=false});

          for (int i = 0; i < countrylist.Count; i++)
          {
          if (i % 2 == 0)
          {
          countrylist[i].backgroundColor = Color.LightGray;
          }
          else
          {
          countrylist[i].backgroundColor = Color.White;
          }
          }
          LocationListView.ItemsSource = countrylist;
          }

          private void Switch_Toggled(object sender, ToggledEventArgs e)
          {
          Console.WriteLine(" outside switch clicked");
          if(SelectToggled.IsToggled)
          {
          for (Int32 i = 0; i < countrylist.Count; i++)
          {
          countrylist[i].IsToggle = true;
          }
          }
          else
          {
          for (Int32 i = 0; i < countrylist.Count; i++)
          {
          countrylist[i].IsToggle = false;
          }
          }
          }

          private void SwitchList_Toggled(object sender, ToggledEventArgs e)
          {
          Console.WriteLine(" inside switch clicked");
          }





          public class Country:INotifyPropertyChanged
          {
          private string _CountryName;
          public string CountryName
          {
          get { return _CountryName; }
          set
          {
          _CountryName = value;
          RaisePropertyChanged("CountryName");
          }
          }
          private Color _backgroundColor;
          public Color backgroundColor
          {
          get { return _backgroundColor; }
          set
          {
          _backgroundColor = value;
          RaisePropertyChanged("backgroundColor");
          }
          }

          private bool _IsToggle;
          public bool IsToggle
          {
          get { return _IsToggle; }
          set
          {
          _IsToggle = value;
          RaisePropertyChanged("IsToggle");
          }
          }

          public event PropertyChangedEventHandler PropertyChanged;

          public void RaisePropertyChanged(string propertyName)
          {
          PropertyChangedEventHandler handler = PropertyChanged;
          if(handler!=null)
          {
          handler(this,new PropertyChangedEventArgs(propertyName));
          }
          }
          }





          share|improve this answer












          You can foreach through CountryList, I do one sample that you can take a look:



          <?xml version="1.0" encoding="utf-8" ?>
          <ContentPage
          x:Class="Test1.MainPage"
          xmlns="http://xamarin.com/schemas/2014/forms"
          xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
          xmlns:local="clr-namespace:Test1">

          <StackLayout>
          <Label
          Margin="0,0,10,0"
          FontSize="Medium"
          HorizontalOptions="End"
          Text="Select All"
          TextColor="White"
          VerticalOptions="Center" />
          <Switch
          x:Name="SelectToggled"
          Margin="0,0,10,0"
          BackgroundColor="BlueViolet"
          HorizontalOptions="End"
          Toggled="Switch_Toggled"
          VerticalOptions="Center" />

          <ListView
          x:Name="LocationListView"
          BackgroundColor="AliceBlue"
          HasUnevenRows="True"
          SeparatorVisibility="None"
          VerticalOptions="FillAndExpand">
          <ListView.ItemTemplate>
          <DataTemplate>
          <ViewCell>
          <Grid
          Margin="0,0,0,0"
          Padding="20,0,0,0"
          BackgroundColor="{Binding backgroundColor}"
          HeightRequest="47"
          HorizontalOptions="FillAndExpand"
          RowSpacing="0">
          <Grid.RowDefinitions>
          <RowDefinition Height="*" />
          </Grid.RowDefinitions>
          <Grid.ColumnDefinitions>
          <ColumnDefinition Width="80*" />
          <ColumnDefinition Width="20*" />
          </Grid.ColumnDefinitions>
          <Label
          Grid.Row="0"
          Grid.Column="0"
          FontFamily="Roboto"
          FontSize="Medium"
          HorizontalOptions="Start"
          Text="{Binding CountryName}"
          TextColor="Black"
          VerticalOptions="Center" />
          <Switch
          x:Name="switch1"
          Grid.Row="0"
          Grid.Column="1"
          HorizontalOptions="CenterAndExpand"
          IsToggled="{Binding IsToggle, Mode=TwoWay}"
          Toggled="SwitchList_Toggled"
          VerticalOptions="CenterAndExpand" />
          </Grid>
          </ViewCell>
          </DataTemplate>
          </ListView.ItemTemplate>
          </ListView>
          </StackLayout>




          public ObservableCollection<Country> countrylist { get; set; }
          public MainPage()
          {
          InitializeComponent();

          countrylist = new ObservableCollection<Country>();
          countrylist.Add(new Country { CountryName = "India",IsToggle=false });
          countrylist.Add(new Country { CountryName = "America" ,IsToggle=true});
          countrylist.Add(new Country { CountryName = "England" ,IsToggle=false});

          for (int i = 0; i < countrylist.Count; i++)
          {
          if (i % 2 == 0)
          {
          countrylist[i].backgroundColor = Color.LightGray;
          }
          else
          {
          countrylist[i].backgroundColor = Color.White;
          }
          }
          LocationListView.ItemsSource = countrylist;
          }

          private void Switch_Toggled(object sender, ToggledEventArgs e)
          {
          Console.WriteLine(" outside switch clicked");
          if(SelectToggled.IsToggled)
          {
          for (Int32 i = 0; i < countrylist.Count; i++)
          {
          countrylist[i].IsToggle = true;
          }
          }
          else
          {
          for (Int32 i = 0; i < countrylist.Count; i++)
          {
          countrylist[i].IsToggle = false;
          }
          }
          }

          private void SwitchList_Toggled(object sender, ToggledEventArgs e)
          {
          Console.WriteLine(" inside switch clicked");
          }





          public class Country:INotifyPropertyChanged
          {
          private string _CountryName;
          public string CountryName
          {
          get { return _CountryName; }
          set
          {
          _CountryName = value;
          RaisePropertyChanged("CountryName");
          }
          }
          private Color _backgroundColor;
          public Color backgroundColor
          {
          get { return _backgroundColor; }
          set
          {
          _backgroundColor = value;
          RaisePropertyChanged("backgroundColor");
          }
          }

          private bool _IsToggle;
          public bool IsToggle
          {
          get { return _IsToggle; }
          set
          {
          _IsToggle = value;
          RaisePropertyChanged("IsToggle");
          }
          }

          public event PropertyChangedEventHandler PropertyChanged;

          public void RaisePropertyChanged(string propertyName)
          {
          PropertyChangedEventHandler handler = PropertyChanged;
          if(handler!=null)
          {
          handler(this,new PropertyChangedEventArgs(propertyName));
          }
          }
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 8 at 4:44









          Cherry Bu

          84628




          84628












          • Hi Cherry, Thank your reply. But I need the following...
            – Arun manthiram
            Nov 9 at 13:39












          • When i Switched On the "Outside switch control", all "inside switch controls" should be switched On. When I Switched Off the "Outside switch control", all "inside switch controls" should be switched Off. When I switched Off/On any switch control in "inside switch Control",then the "outside switch control" should be automatically switched Off/On.
            – Arun manthiram
            Nov 9 at 13:39










          • @Arun, For When I switched Off/On any switch control in "inside switch Control",then the "outside switch control" should be automatically switched Off/On, I think it is not unreasonable, because When you switched Off/On any switch control in "inside switch Control",then the "outside switch control" should be automatically switched Off/On, then all "inside switch controls" should be switched On. . It is meaningless.
            – Cherry Bu
            Nov 12 at 7:21




















          • Hi Cherry, Thank your reply. But I need the following...
            – Arun manthiram
            Nov 9 at 13:39












          • When i Switched On the "Outside switch control", all "inside switch controls" should be switched On. When I Switched Off the "Outside switch control", all "inside switch controls" should be switched Off. When I switched Off/On any switch control in "inside switch Control",then the "outside switch control" should be automatically switched Off/On.
            – Arun manthiram
            Nov 9 at 13:39










          • @Arun, For When I switched Off/On any switch control in "inside switch Control",then the "outside switch control" should be automatically switched Off/On, I think it is not unreasonable, because When you switched Off/On any switch control in "inside switch Control",then the "outside switch control" should be automatically switched Off/On, then all "inside switch controls" should be switched On. . It is meaningless.
            – Cherry Bu
            Nov 12 at 7:21


















          Hi Cherry, Thank your reply. But I need the following...
          – Arun manthiram
          Nov 9 at 13:39






          Hi Cherry, Thank your reply. But I need the following...
          – Arun manthiram
          Nov 9 at 13:39














          When i Switched On the "Outside switch control", all "inside switch controls" should be switched On. When I Switched Off the "Outside switch control", all "inside switch controls" should be switched Off. When I switched Off/On any switch control in "inside switch Control",then the "outside switch control" should be automatically switched Off/On.
          – Arun manthiram
          Nov 9 at 13:39




          When i Switched On the "Outside switch control", all "inside switch controls" should be switched On. When I Switched Off the "Outside switch control", all "inside switch controls" should be switched Off. When I switched Off/On any switch control in "inside switch Control",then the "outside switch control" should be automatically switched Off/On.
          – Arun manthiram
          Nov 9 at 13:39












          @Arun, For When I switched Off/On any switch control in "inside switch Control",then the "outside switch control" should be automatically switched Off/On, I think it is not unreasonable, because When you switched Off/On any switch control in "inside switch Control",then the "outside switch control" should be automatically switched Off/On, then all "inside switch controls" should be switched On. . It is meaningless.
          – Cherry Bu
          Nov 12 at 7:21






          @Arun, For When I switched Off/On any switch control in "inside switch Control",then the "outside switch control" should be automatically switched Off/On, I think it is not unreasonable, because When you switched Off/On any switch control in "inside switch Control",then the "outside switch control" should be automatically switched Off/On, then all "inside switch controls" should be switched On. . It is meaningless.
          – Cherry Bu
          Nov 12 at 7:21




















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53189391%2fwhen-i-click-the-event-on-outside-of-listview-switch-button-or-inside-of-listvie%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Xamarin.form Move up view when keyboard appear

          Post-Redirect-Get with Spring WebFlux and Thymeleaf

          Anylogic : not able to use stopDelay()