问题
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.
回答1:
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));
}
}
}
来源:https://stackoverflow.com/questions/53189391/when-i-click-the-event-on-outside-of-listview-switch-button-or-inside-of-listvie