Collectionview is broken when I change grid - xamarin

I have a list where can be one or more than grid. I am trying to use collectionview for this. But when I change grid, view of some collectionview items are broken (on iOS app). As in gif:
But when I swipe scroll, broken grids are getting better. My project is custom project. That's why I can't share sample app. But collectionview implementation is as follows:
<RefreshView Command="{Binding RefreshCommand}"
IsRefreshing="{Binding IsRefreshing}">
<CollectionView x:Name="FlowListFileFolder"
AutomationId="FileSystemPage_MainScreenListView"
IsGrouped="true"
ItemsSource="{Binding FlowListCloudItem}"
ItemsLayout="{Binding CollectionViewItemsLayout}">
<CollectionView.GroupHeaderTemplate>
<DataTemplate>
<StackLayout Style="{DynamicResource FlowListGroupTitleWrapperStyle}">
<Label Text="{Binding Path=Key}"
Style="{DynamicResource FlowListGroupTitleStyle}" />
</StackLayout>
</DataTemplate>
</CollectionView.GroupHeaderTemplate>
<CollectionView.ItemTemplate>
<DataTemplate>
<local:ItemGridView LongPressEvent="Handle_LongPressEvent" SingleTapEvent="Handle_TappedEvent">
<local:CustomFolderFrame Style="{DynamicResource FspCiFrameWrapperStyle}" BorderColor="{Binding FrameColor}">
<StackLayout Orientation="Vertical" Spacing="0" BackgroundColor="{Binding FrameColor}" Margin="1">
<Grid Style="{Binding FspCi_GridStyle}">
<StackLayout AutomationId="FileSystemPage_FolderStackLayout"
Style="{DynamicResource FspCiGeneralWrapperLayoutStyle}">
<StackLayout Style="{Binding FspCiThumbnailImageAndInformation_GeneralWrapperLayoutStyle}">
<StackLayout Style="{Binding FspCiThumbnailImage_WrapperLayoutStyle}">
<ff:CachedImage AutomationId="{Binding LvItemThumbnailSource, StringFormat='FolderIcon_{0}'}"
Style="{Binding FspCiFolderThumbnailImageStyle}" />
<ff:CachedImage Style="{Binding FspCiFileThumbnailImageStyle}" />
</StackLayout>
<StackLayout Style="{Binding FspCiCloudItemInformation_WrapperLayoutStyle}">
<StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand" Spacing="5">
<Label AutomationId="FileSystemPage_lblDisplayNameFolder"
Text="{Binding DisplayName}"
Style="{DynamicResource FspCiDisplayNameStyle}" />
<ff:CachedImage Source="group.png" Style="{Binding FspCiGroupIcon_ImageStyle}" />
</StackLayout>
<Label Text="{Binding CreationDate}"
Style="{DynamicResource FspCiCreationDateStyle}" />
</StackLayout>
</StackLayout>
</StackLayout>
<StackLayout Style="{Binding FspCiCheckUncheckMoreDetail_WrapperLayoutStyle}">
<StackLayout Style="{Binding FspCiCheck_StackLayoutStyle}">
<ff:CachedImage AutomationId="{Binding DisplayName, StringFormat='FileSystemPage_{0}_checked'}"
Source="checked.png"
Style="{Binding FspCiCheckUncheck_ImageStyle}" />
</StackLayout>
<StackLayout Style="{Binding FspCiUncheck_StackLayoutStyle}">
<ff:CachedImage AutomationId="{Binding DisplayName, StringFormat='FileSystemPage_{0}_unchecked'}"
Source="unchecked.png"
Style="{Binding FspCiCheckUncheck_ImageStyle}" />
</StackLayout>
<StackLayout Style="{Binding FspCiMoreDetail_StackLayoutStyle}">
<ff:CachedImage AutomationId="FileSystemPage_ItemMoreDetail"
Source="ic_dots_gray.png"
Style="{Binding FspCiCheckUncheck_ImageStyle}"/>
<StackLayout.GestureRecognizers>
<TapGestureRecognizer Tapped="ItemMoreDetail_Clicked" />
</StackLayout.GestureRecognizers>
</StackLayout>
</StackLayout>
</Grid>
</StackLayout>
</local:CustomFolderFrame>
</local:ItemGridView>
</DataTemplate>
</CollectionView.ItemTemplate>
<CollectionView.Footer>
<StackLayout Style="{DynamicResource FspCiFooterWrapperLayoutStyle}">
<Label Text="{Binding LabelFolderCount}"
Style="{DynamicResource FspCiFooterFileFolderCountStyle}" />
<Label Text="{Binding LabelFileCount}"
Style="{DynamicResource FspCiFooterFileFolderCountStyle}" />
</StackLayout>
</CollectionView.Footer>
</CollectionView>
</RefreshView>
For supporting different screens with one project, I had used different styles that changes according to device's viewport information. When app open, I gets device's viewport information and I set style that is suitable viewport. These styles gets from there.
And when I click grid button, I change some of these styles as follow:
public class FileSystemViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
protected void SetValue<T>(ref T backingField, T value, [CallerMemberName] string propertyName = null)
{
if (EqualityComparer<T>.Default.Equals(backingField, value))
return;
backingField = value;
OnPropertyChanged(propertyName);
}
private ItemsLayout collectionViewItemsLayout;
public ItemsLayout CollectionViewItemsLayout { get => collectionViewItemsLayout; set { SetValue(ref collectionViewItemsLayout, value); } }
public Command GridClicked()
{
var btnGrid_Clicked = new Command(
async () => {
if (FlowListGridCount == 1)
{
UpdateMultipleGrid();
}
else
{
UpdateOneGrid();
}
});
return btnGrid_Clicked;
}
public void UpdateMultipleGrid()
{
foreach (var item in CloudItemList[0])
{
SetFolderTwoGridView(item);
}
foreach (var item in CloudItemList[1])
{
SetFileTwoGridView(item);
}
Helpers.Settings.FlowColumnCount = FlowListGridCount = ElementResolutionsHelper.GetPageFolderColumnCount(); //GetPageFolderColumnCount returns column count that is suitable with screen
CollectionViewItemsLayout = new GridItemsLayout(FlowListGridCount, ItemsLayoutOrientation.Vertical);
}
public void UpdateOneGrid()
{
foreach (var item in CloudItemList[0])
{
SetFolderOneGridView(item);
}
foreach (var item in CloudItemList[1])
{
SetFileOneGridView(item);
}
Helpers.Settings.FlowColumnCount = FlowListGridCount = ElementResolutionsHelper.GetPageFolderColumnCount(); //GetPageFolderColumnCount returns 1 for this situation
CollectionViewItemsLayout = new GridItemsLayout(FlowListGridCount, ItemsLayoutOrientation.Vertical);
}
private void SetFolderTwoGridView(CloudItemViewingResponse item)
{
if (item.IsShared)
item.LvItemThumbnailSource = "folder_share.png";
else
item.LvItemThumbnailSource = "ic_folder.png";
item.FspCiCheck_StackLayoutStyle = (Style)((Application.Current.Resources.MergedDictionaries).ElementAt((int)EStyleItem.DevicesStyle)["FspCiMultipleGridCheck_StackLayoutStyle"]);
item.FspCiUncheck_StackLayoutStyle = (Style)((Application.Current.Resources.MergedDictionaries).ElementAt((int)EStyleItem.DevicesStyle)["FspCiMultipleGridUncheck_StackLayoutStyle"]);
item.FspCiMoreDetail_StackLayoutStyle = (Style)((Application.Current.Resources.MergedDictionaries).ElementAt((int)EStyleItem.DevicesStyle)["FspCiMultipleGridMoreDetail_StackLayoutStyle"]);
item.FspCiCheckUncheck_ImageStyle = (Style)((Application.Current.Resources.MergedDictionaries).ElementAt((int)EStyleItem.DevicesStyle)["FspCiMultipleGridCheckUncheckMoreDetail_ImageStyle"]);
item.FspCiCheckUncheckMoreDetail_WrapperLayoutStyle = (Style)((Application.Current.Resources.MergedDictionaries).ElementAt((int)EStyleItem.DevicesStyle)["FspCiMultipleGridCheckUncheckMoreDetail_WrapperLayoutStyle"]);
item.FspCiCloudItemInformation_WrapperLayoutStyle = (Style)((Application.Current.Resources.MergedDictionaries).ElementAt((int)EStyleItem.DevicesStyle)["FspCiMultipleGridCloudItemInformation_WrapperLayoutStyle"]);
item.FspCiThumbnailImage_WrapperLayoutStyle = (Style)((Application.Current.Resources.MergedDictionaries).ElementAt((int)EStyleItem.DevicesStyle)["FspCiMultipleGridFolderThumbnailImage_WrapperLayoutStyle"]);
item.FspCiFolderThumbnailImageStyle = (Style)((Application.Current.Resources.MergedDictionaries).ElementAt((int)EStyleItem.DevicesStyle)["FspCiMultipleGridFolderThumbnailImageStyle"]);
item.FspCiFileThumbnailImageStyle = (Style)((Application.Current.Resources.MergedDictionaries).ElementAt((int)EStyleItem.DevicesStyle)["FspCiMultipleGridFileThumbnailImageStyle"]);
item.FspCiThumbnailImageAndInformation_GeneralWrapperLayoutStyle = (Style)((Application.Current.Resources.MergedDictionaries).ElementAt((int)EStyleItem.DevicesStyle)["FspCiMultipleGridThumbnailImageAndInformation_GeneralWrapperLayoutStyle"]);
item.FspCi_GridStyle = (Style)((Application.Current.Resources.MergedDictionaries).ElementAt((int)EStyleItem.DevicesStyle)["FspCiMultipleGridFolder_GridStyle"]);
}
private void SetFileTwoGridView(CloudItemViewingResponse item)
{
item.FspCiCheck_StackLayoutStyle = (Style)((Application.Current.Resources.MergedDictionaries).ElementAt((int)EStyleItem.DevicesStyle)["FspCiMultipleGridCheck_StackLayoutStyle"]);
item.FspCiUncheck_StackLayoutStyle = (Style)((Application.Current.Resources.MergedDictionaries).ElementAt((int)EStyleItem.DevicesStyle)["FspCiMultipleGridUncheck_StackLayoutStyle"]);
item.FspCiMoreDetail_StackLayoutStyle = (Style)((Application.Current.Resources.MergedDictionaries).ElementAt((int)EStyleItem.DevicesStyle)["FspCiMultipleGridMoreDetail_StackLayoutStyle"]);
item.FspCiCheckUncheck_ImageStyle = (Style)((Application.Current.Resources.MergedDictionaries).ElementAt((int)EStyleItem.DevicesStyle)["FspCiMultipleGridCheckUncheckMoreDetail_ImageStyle"]);
item.FspCiCheckUncheckMoreDetail_WrapperLayoutStyle = (Style)((Application.Current.Resources.MergedDictionaries).ElementAt((int)EStyleItem.DevicesStyle)["FspCiMultipleGridCheckUncheckMoreDetail_WrapperLayoutStyle"]);
item.FspCiCloudItemInformation_WrapperLayoutStyle = (Style)((Application.Current.Resources.MergedDictionaries).ElementAt((int)EStyleItem.DevicesStyle)["FspCiMultipleGridCloudItemInformation_WrapperLayoutStyle"]);
item.FspCiThumbnailImage_WrapperLayoutStyle = (Style)((Application.Current.Resources.MergedDictionaries).ElementAt((int)EStyleItem.DevicesStyle)["FspCiMultipleGridFileThumbnailImage_WrapperLayoutStyle"]);
item.FspCiFolderThumbnailImageStyle = (Style)((Application.Current.Resources.MergedDictionaries).ElementAt((int)EStyleItem.DevicesStyle)["FspCiMultipleGridFolderThumbnailImageStyle"]);
item.FspCiFileThumbnailImageStyle = (Style)((Application.Current.Resources.MergedDictionaries).ElementAt((int)EStyleItem.DevicesStyle)["FspCiMultipleGridFileThumbnailImageStyle"]);
item.FspCiThumbnailImageAndInformation_GeneralWrapperLayoutStyle = (Style)((Application.Current.Resources.MergedDictionaries).ElementAt((int)EStyleItem.DevicesStyle)["FspCiMultipleGridThumbnailImageAndInformation_GeneralWrapperLayoutStyle"]);
item.FspCi_GridStyle = (Style)((Application.Current.Resources.MergedDictionaries).ElementAt((int)EStyleItem.DevicesStyle)["FspCiMultipleGridFile_GridStyle"]);
}
private void SetFolderOneGridView(CloudItemViewingResponse item)
{
if (item.IsShared)
item.LvItemThumbnailSource = "folder_share.png";
else
item.LvItemThumbnailSource = "ic_folder.png";
item.FspCiCheck_StackLayoutStyle = (Style)((Application.Current.Resources.MergedDictionaries).ElementAt((int)EStyleItem.DevicesStyle)["FspCiOneGridCheck_StackLayoutStyle"]);
item.FspCiUncheck_StackLayoutStyle = (Style)((Application.Current.Resources.MergedDictionaries).ElementAt((int)EStyleItem.DevicesStyle)["FspCiOneGridUncheck_StackLayoutStyle"]);
item.FspCiMoreDetail_StackLayoutStyle = (Style)((Application.Current.Resources.MergedDictionaries).ElementAt((int)EStyleItem.DevicesStyle)["FspCiOneGridMoreDetail_StackLayoutStyle"]);
item.FspCiCheckUncheck_ImageStyle = (Style)((Application.Current.Resources.MergedDictionaries).ElementAt((int)EStyleItem.DevicesStyle)["FspCiOneGridCheckUncheckMoreDetail_ImageStyle"]);
item.FspCiCheckUncheckMoreDetail_WrapperLayoutStyle = (Style)((Application.Current.Resources.MergedDictionaries).ElementAt((int)EStyleItem.DevicesStyle)["FspCiOneGridCheckUncheckMoreDetail_WrapperLayoutStyle"]);
item.FspCiCloudItemInformation_WrapperLayoutStyle = (Style)((Application.Current.Resources.MergedDictionaries).ElementAt((int)EStyleItem.DevicesStyle)["FspCiOneGridCloudItemInformation_WrapperLayoutStyle"]);
item.FspCiThumbnailImage_WrapperLayoutStyle = (Style)((Application.Current.Resources.MergedDictionaries).ElementAt((int)EStyleItem.DevicesStyle)["FspCiOneGridThumbnailImage_WrapperLayoutStyle"]);
item.FspCiFolderThumbnailImageStyle = (Style)((Application.Current.Resources.MergedDictionaries).ElementAt((int)EStyleItem.DevicesStyle)["FspCiOneGridFolderThumbnailImageStyle"]);
item.FspCiFileThumbnailImageStyle = (Style)((Application.Current.Resources.MergedDictionaries).ElementAt((int)EStyleItem.DevicesStyle)["FspCiOneGridFileThumbnailImageStyle"]);
item.FspCiThumbnailImageAndInformation_GeneralWrapperLayoutStyle = (Style)((Application.Current.Resources.MergedDictionaries).ElementAt((int)EStyleItem.DevicesStyle)["FspCiOneGridThumbnailImageAndInformation_GeneralWrapperLayoutStyle"]);
item.FspCi_GridStyle = (Style)((Application.Current.Resources.MergedDictionaries).ElementAt((int)EStyleItem.DevicesStyle)["FspCiOneGrid_GridStyle"]);
}
private void SetFileOneGridView(CloudItemViewingResponse item)
{
item.FspCiCheck_StackLayoutStyle = (Style)((Application.Current.Resources.MergedDictionaries).ElementAt((int)EStyleItem.DevicesStyle)["FspCiOneGridCheck_StackLayoutStyle"]);
item.FspCiUncheck_StackLayoutStyle = (Style)((Application.Current.Resources.MergedDictionaries).ElementAt((int)EStyleItem.DevicesStyle)["FspCiOneGridUncheck_StackLayoutStyle"]);
item.FspCiMoreDetail_StackLayoutStyle = (Style)((Application.Current.Resources.MergedDictionaries).ElementAt((int)EStyleItem.DevicesStyle)["FspCiOneGridMoreDetail_StackLayoutStyle"]);
item.FspCiCheckUncheck_ImageStyle = (Style)((Application.Current.Resources.MergedDictionaries).ElementAt((int)EStyleItem.DevicesStyle)["FspCiOneGridCheckUncheckMoreDetail_ImageStyle"]);
item.FspCiCheckUncheckMoreDetail_WrapperLayoutStyle = (Style)((Application.Current.Resources.MergedDictionaries).ElementAt((int)EStyleItem.DevicesStyle)["FspCiOneGridCheckUncheckMoreDetail_WrapperLayoutStyle"]);
item.FspCiCloudItemInformation_WrapperLayoutStyle = (Style)((Application.Current.Resources.MergedDictionaries).ElementAt((int)EStyleItem.DevicesStyle)["FspCiOneGridCloudItemInformation_WrapperLayoutStyle"]);
item.FspCiThumbnailImage_WrapperLayoutStyle = (Style)((Application.Current.Resources.MergedDictionaries).ElementAt((int)EStyleItem.DevicesStyle)["FspCiOneGridThumbnailImage_WrapperLayoutStyle"]);
item.FspCiFolderThumbnailImageStyle = (Style)((Application.Current.Resources.MergedDictionaries).ElementAt((int)EStyleItem.DevicesStyle)["FspCiOneGridFolderThumbnailImageStyle"]);
item.FspCiFileThumbnailImageStyle = (Style)((Application.Current.Resources.MergedDictionaries).ElementAt((int)EStyleItem.DevicesStyle)["FspCiOneGridFileThumbnailImageStyle"]);
item.FspCiThumbnailImageAndInformation_GeneralWrapperLayoutStyle = (Style)((Application.Current.Resources.MergedDictionaries).ElementAt((int)EStyleItem.DevicesStyle)["FspCiOneGridThumbnailImageAndInformation_GeneralWrapperLayoutStyle"]);
item.FspCi_GridStyle = (Style)((Application.Current.Resources.MergedDictionaries).ElementAt((int)EStyleItem.DevicesStyle)["FspCiOneGrid_GridStyle"]);
}
}
We would change the HasUnevenRows or CachingStrategy value for the listview in case of such broken. Does collectionview have something like this?
And collectionview grid doesn't broken on Android. But when I change grid, it turns back at the top of the list. If I swipe to bottom, I don't want to turn back to top when I change grid.
Thank you in advance.

Related

PropertyChanged Event doesn't reflect new Image.Source on first run

I'm working on mobile app that kinda simulates how to build a Desktop Computer by dragging and dropping an image in order. I work with DropGestureRecognizer with AllowDrop set to True for the Drop Zone Image controls and DragGestureRecognizer with CanDrag set to True for the Drag Image Objects.
The idea is the Drag Image Objects will get dragged and dropped to a Drop Zone Image Control and if they dropped the correct Drag Image, the Drop Zone will accept this as the Image.Source and another Drop Zone will be stacked in front of it to work with the next Desktop Computer component. Otherwise, it would get rejected and the Image.Source will be set to empty or null.
However, i'm encountering a weird issue that, on the first incorrect image dragged, the Image.Source gets reset to empty or null by property but on the actual UI, it doesn't. On the succeeding EventHandler execution, it does get reset. I have a TapGestureRecognizer on the Drop Zone to check if there is an Image.Source set or not.
I'm sorry if my explanation is a bit confusing because English is not my native language and i'm having a hard time explaining it. So please refer to below example in GIF and my code:
View (.xaml):
<ContentPage.Content>
<Grid RowDefinitions="Auto,60,*"
ColumnDefinitions="*">
<RelativeLayout Grid.Row="0" Grid.Column="0" HorizontalOptions="Center">
<Image x:Name="imgCaseDropZone"
RelativeLayout.XConstraint="0"
RelativeLayout.YConstraint="0"
HeightRequest="{Binding ScreenWidth}"
WidthRequest="{Binding ScreenWidth}"
PropertyChanged="CaseDropZone_PropertyChanged"
BackgroundColor="LightGray">
<Image.GestureRecognizers>
<DropGestureRecognizer AllowDrop="True" />
<TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped" />
</Image.GestureRecognizers>
</Image>
</RelativeLayout>
<Label Grid.Row="1" Grid.Column="0"
x:Name="lblDirections"
Text="Directions: Drag the components below in its proper order to the drop zone above."
TextColor="Black"
Padding="10"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand" />
<ScrollView Grid.Row="2"
Orientation="Horizontal"
Margin="5">
<StackLayout Orientation="Horizontal">
<!--#region Case -->
<Grid RowDefinitions="*,Auto"
IsVisible="{Binding CaseIsVisible}">
<Image Grid.Row="0"
x:Name="imgCaseDragObject"
Source="{Binding CaseImgSource}">
<Image.GestureRecognizers>
<DragGestureRecognizer CanDrag="True" />
</Image.GestureRecognizers>
</Image>
<Label Grid.Row="1"
Text="{Binding CaseLabel}"
TextColor="White"
FontSize="Medium"
BackgroundColor="Gray"
HorizontalTextAlignment="Center" />
</Grid>
<!--#endregion-->
<!--#region Case Cover -->
<Grid RowDefinitions="*,Auto"
IsVisible="{Binding CaseCoverIsVisible}">
<Image Grid.Row="0"
Source="{Binding CaseCoverImgSource}">
<Image.GestureRecognizers>
<DragGestureRecognizer CanDrag="True" />
</Image.GestureRecognizers>
</Image>
<Label Grid.Row="1"
Text="{Binding CaseCoverLabel}"
TextColor="White"
FontSize="Medium"
BackgroundColor="Gray"
HorizontalTextAlignment="Center" />
</Grid>
<!--#endregion-->
<!--#region Case Screw -->
<Grid RowDefinitions="*,Auto"
IsVisible="{Binding CaseScrewIsVisible}">
<Image Grid.Row="0"
Source="{Binding CaseScrewImgSource}">
<Image.GestureRecognizers>
<DragGestureRecognizer CanDrag="True" />
</Image.GestureRecognizers>
</Image>
<Label Grid.Row="1"
Text="{Binding CaseScrewLabel}"
TextColor="White"
FontSize="Medium"
BackgroundColor="Gray"
HorizontalTextAlignment="Center" />
</Grid>
<!--#endregion-->
<!--#region Hard Disk Drive -->
<Grid RowDefinitions="*,Auto"
IsVisible="{Binding HardDiskDriveIsVisible}">
<Image Grid.Row="0"
Source="{Binding HardDiskDriveImgSource}">
<Image.GestureRecognizers>
<DragGestureRecognizer CanDrag="True" />
</Image.GestureRecognizers>
</Image>
<Label Grid.Row="1"
Text="{Binding HardDiskDriveLabel}"
TextColor="White"
FontSize="Medium"
BackgroundColor="Gray"
HorizontalTextAlignment="Center" />
</Grid>
<!--#endregion-->
<!--#region Heatsink -->
<Grid RowDefinitions="*,Auto"
IsVisible="{Binding HeatsinkIsVisible}">
<Image Grid.Row="0"
Source="{Binding HeatsinkImgSource}">
<Image.GestureRecognizers>
<DragGestureRecognizer CanDrag="True" />
</Image.GestureRecognizers>
</Image>
<Label Grid.Row="1"
Text="{Binding HeatsinkLabel}"
TextColor="White"
FontSize="Medium"
BackgroundColor="Gray"
HorizontalTextAlignment="Center" />
</Grid>
<!--#endregion-->
<!--#region Memory Module -->
<Grid RowDefinitions="*,Auto"
IsVisible="{Binding MemoryModuleIsVisible}">
<Image Grid.Row="0"
Source="{Binding MemoryModuleImgSource}">
<Image.GestureRecognizers>
<DragGestureRecognizer CanDrag="True" />
</Image.GestureRecognizers>
</Image>
<Label Grid.Row="1"
Text="{Binding MemoryModuleLabel}"
TextColor="White"
FontSize="Medium"
BackgroundColor="Gray"
HorizontalTextAlignment="Center" />
</Grid>
<!--#endregion-->
<!--#region Motherboard -->
<Grid RowDefinitions="*,Auto"
IsVisible="{Binding MotherboardIsVisible}">
<Image Grid.Row="0"
Source="{Binding MotherboardImgSource}">
<Image.GestureRecognizers>
<DragGestureRecognizer CanDrag="True" />
</Image.GestureRecognizers>
</Image>
<Label Grid.Row="1"
Text="{Binding MotherboardLabel}"
TextColor="White"
FontSize="Medium"
BackgroundColor="Gray"
HorizontalTextAlignment="Center" />
</Grid>
<!--#endregion-->
<!--#region Motherboard Screw -->
<Grid RowDefinitions="*,Auto"
IsVisible="{Binding MotherboardScrewIsVisible}">
<Image Grid.Row="0"
Source="{Binding MotherboardScrewImgSource}">
<Image.GestureRecognizers>
<DragGestureRecognizer CanDrag="True" />
</Image.GestureRecognizers>
</Image>
<Label Grid.Row="1"
Text="{Binding MotherboardScrewLabel}"
TextColor="White"
FontSize="Medium"
BackgroundColor="Gray"
HorizontalTextAlignment="Center" />
</Grid>
<!--#endregion-->
<!--#region Power Supply -->
<Grid RowDefinitions="*,Auto"
IsVisible="{Binding PowerSupplyIsVisible}">
<Image Grid.Row="0"
Source="{Binding PowerSupplyImgSource}">
<Image.GestureRecognizers>
<DragGestureRecognizer CanDrag="True" />
</Image.GestureRecognizers>
</Image>
<Label Grid.Row="1"
Text="{Binding PowerSupplyLabel}"
TextColor="White"
FontSize="Medium"
BackgroundColor="Gray"
HorizontalTextAlignment="Center" />
</Grid>
<!--#endregion-->
<!--#region Processor -->
<Grid RowDefinitions="*,Auto"
IsVisible="{Binding ProcessorIsVisible}">
<Image Grid.Row="0"
Source="{Binding ProcessorImgSource}">
<Image.GestureRecognizers>
<DragGestureRecognizer CanDrag="True" />
</Image.GestureRecognizers>
</Image>
<Label Grid.Row="1"
Text="{Binding ProcessorLabel}"
TextColor="White"
FontSize="Medium"
BackgroundColor="Gray"
HorizontalTextAlignment="Center" />
</Grid>
<!--#endregion-->
</StackLayout>
</ScrollView>
</Grid>
</ContentPage.Content>
View Code (.xaml.cs):
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class AssemblyPage : ContentPage
{
string caseSource;
public AssemblyPage()
{
InitializeComponent();
}
int dragCount = 0;
private void CaseDropZone_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "Source")
{
caseSource = imgCaseDropZone.Source.ToString().Split(':').Last().Trim();
if (string.IsNullOrEmpty(caseSource))
{
return;
}
if (caseSource != "img_assembly_case.png")
{
imgCaseDropZone.Source = string.Empty;
lblDirections.Text = $"Drag Count: {++dragCount}";
}
}
}
private void TapGestureRecognizer_Tapped(object sender, EventArgs e)
{
Application.Current.MainPage.DisplayAlert("", $"{imgCaseDropZone.Source}", "OK");
}
}
ViewModel (.cs):
public class AssemblyViewModel : BaseVM
{
public double ScreenWidth => Xamarin.Forms.Application.Current.MainPage.Width;
#region Case
bool caseIsVisible;
public bool CaseIsVisible
{
get => caseIsVisible;
set => SetProperty(ref caseIsVisible, value);
}
public string CaseImgSource { get; }
public string CaseLabel { get; }
#endregion
#region CaseCover
bool caseCoverIsVisible;
public bool CaseCoverIsVisible
{
get => caseCoverIsVisible;
set => SetProperty(ref caseCoverIsVisible, value);
}
public string CaseCoverImgSource { get; }
public string CaseCoverLabel { get; }
#endregion
#region HardDiskDrive
bool hardDiskDriveIsVisible;
public bool HardDiskDriveIsVisible
{
get => hardDiskDriveIsVisible;
set => SetProperty(ref hardDiskDriveIsVisible, value);
}
public string HardDiskDriveImgSource { get; }
public string HardDiskDriveLabel { get; }
#endregion
#region CaseScrew
bool caseScrewIsVisible;
public bool CaseScrewIsVisible
{
get => caseScrewIsVisible;
set => SetProperty(ref caseScrewIsVisible, value);
}
public string CaseScrewImgSource { get; }
public string CaseScrewLabel { get; }
#endregion
#region Heatsink
bool heatsinkIsVisible;
public bool HeatsinkIsVisible
{
get => heatsinkIsVisible;
set => SetProperty(ref heatsinkIsVisible, value);
}
public string HeatsinkImgSource { get; }
public string HeatsinkLabel { get; }
#endregion
#region MemoryModule
bool memoryModuleIsVisible;
public bool MemoryModuleIsVisible
{
get => memoryModuleIsVisible;
set => SetProperty(ref memoryModuleIsVisible, value);
}
public string MemoryModuleImgSource { get; }
public string MemoryModuleLabel { get; }
#endregion
#region Motherboard
bool motherboardIsVisible;
public bool MotherboardIsVisible
{
get => motherboardIsVisible;
set => SetProperty(ref motherboardIsVisible, value);
}
public string MotherboardImgSource { get; }
public string MotherboardLabel { get; }
#endregion
#region MotherboardScrew
bool motherboardScrewIsVisible;
public bool MotherboardScrewIsVisible
{
get => motherboardScrewIsVisible;
set => SetProperty(ref motherboardScrewIsVisible, value);
}
public string MotherboardScrewImgSource { get; }
public string MotherboardScrewLabel { get; }
#endregion
#region PowerSupply
bool powerSupplyIsVisible;
public bool PowerSupplyIsVisible
{
get => powerSupplyIsVisible;
set => SetProperty(ref powerSupplyIsVisible, value);
}
public string PowerSupplyImgSource { get; }
public string PowerSupplyLabel { get; }
#endregion
#region Processor
bool processorIsVisible;
public bool ProcessorIsVisible
{
get => processorIsVisible;
set => SetProperty(ref processorIsVisible, value);
}
public string ProcessorImgSource { get; }
public string ProcessorLabel { get; }
#endregion
public AssemblyViewModel()
{
CaseIsVisible = true;
CaseImgSource = "img_assembly_case.png";
CaseLabel = "Case";
CaseCoverIsVisible = true;
CaseCoverImgSource = "img_assembly_case_cover.png";
CaseCoverLabel = "Case Cover";
CaseScrewIsVisible = true;
CaseScrewImgSource = "img_assembly_case_screw.png";
CaseScrewLabel = "Case Screw";
HardDiskDriveIsVisible = true;
HardDiskDriveImgSource = "img_assembly_hard_disk_drive.png";
HardDiskDriveLabel = "Hard Disk Drive";
HeatsinkIsVisible = true;
HeatsinkImgSource = "img_assembly_heat_sink.png";
HeatsinkLabel = "Heatsink";
MemoryModuleIsVisible = true;
MemoryModuleImgSource = "img_assembly_memory_module.png";
MemoryModuleLabel = "Memory Module";
MotherboardIsVisible = true;
MotherboardImgSource = "img_assembly_motherboard.png";
MotherboardLabel = "Motherboard";
MotherboardScrewIsVisible = true;
MotherboardScrewImgSource = "img_assembly_motherboard_screw.png";
MotherboardScrewLabel = "Motherboard Screw";
PowerSupplyIsVisible = true;
PowerSupplyImgSource = "img_assembly_power_supply.png";
PowerSupplyLabel = "Power Supply";
ProcessorIsVisible = true;
ProcessorImgSource = "img_assembly_processor.png";
ProcessorLabel = "Processor";
}
}
Current Build demo in GIF: https://imgur.com/a/oLeM9DV (i can't directly link the GIF because its too big)
Because when you trigger the PropertyChanged event, the drag and drop action has already been completed and the image has been automatically set to the target.
We could make that judgment as soon as we drag it over the target with DragOver event.Then make an action when the drag and drop is complete with Drop event.
For example,change the codes of the Image (imgCaseDropZone):
<Image x:Name="imgCaseDropZone"
RelativeLayout.XConstraint="0"
RelativeLayout.YConstraint="0"
RelativeLayout.WidthConstraint="400"
RelativeLayout.HeightConstraint="400"
BackgroundColor="LightGray"
>
<Image.GestureRecognizers>
<DropGestureRecognizer AllowDrop="{Binding CaseIsVisible}" DragOver="CaseDropZone_DragOver" Drop="CaseDropZone_Drop"/>
<TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped" />
</Image.GestureRecognizers>
</Image>
in the code behind:
private void CaseDropZone_DragOver(object sender, DragEventArgs e)
{
FileImageSource file = (FileImageSource)e.Data.Image;
string name = file.File;
if (name != "img_assembly_case.png")
{
e.Data.Image = string.Empty; // set the value string.Empty when drag it over the target,then it will fill the empty source to the image.
}
}
private async void CaseDropZone_Drop(object sender, DropEventArgs e)
{
var ctx = (BindingContext as AssemblyViewModel);
FileImageSource source = (FileImageSource)await e.Data.GetImageAsync();
string name = source.File;
if (name == "img_assembly_case.png")
{
ctx.CaseIsVisible = false;
imgMotherboardDropZone.IsVisible = true;
}
else
{
Global.Score.PCAssembly -= 5;
DisplayAlert($"Score: {Global.Score.PCAssembly}", "The pre-requisite component for this part has not yet been placed.", "OK");
}
}
Other Images are modified like this.

Xamarin.forms - checkbox trigger another checkbox's CheckedChanged event

In my xamarin.forms app, I have a listview with checkboxes for the selection of the individual cell. What I am trying to do is multi select the checkboxes inside the listview by providing a "select all" checkbox outside the listview.The Multiselection works fine. For the "select all" checkbox click and individual checkbox click, I am performing some actions like an API Call. The Problem I am facing is Whenever I Click on the "select all" checkbox, the checkbox changed event of individual checkbox gets triggered.I know its natural But is there any way to prevent it like subscribe or unsubscribe the changed event of individual checkbox or something?
Xaml
<Grid >
<Grid.RowDefinitions>
<RowDefinitions Height="Auto"/>
<RowDefinitions Height="Auto"/>
</Grid.RowDefinitions>
<StackLayout Grid.Row="0" Orientation="Horizontal">
<Label Text="Select All" FontSize="Micro" TextColor="LawnGreen" HorizontalOptions="Start" VerticalOptions="Center" >
</Label>
<CheckBox x:Name="MultiselectCheckbox" ScaleX="0.8" ScaleY="0.8" CheckedChanged="MultiSelectCheckBox_CheckedChanged" IsChecked="False" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Color="LawnGreen"></CheckBox>
</StackLayout>
<ListView
x:Name="Listview"
HorizontalOptions="FillAndExpand"
ItemTapped="DistrictList_ItemTapped"
VerticalOptions="FillAndExpand" >
<ListView.ItemTemplate >
<DataTemplate >
<ViewCell >
<ViewCell.View>
<Frame HorizontalOptions="FillAndExpand">
<StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand">
<Label Text="{Binding name}" FontSize="Micro" HorizontalOptions="StartAndExpand" VerticalOptions="Center" TextColor="Snow" Margin="5,0,0,0">
</Label>
<CheckBox CheckedChanged="Single_CheckedChanged" IsChecked="{Binding Selected}" Color="LightBlue" HorizontalOptions="End" >
</CheckBox>
</StackLayout>
</Frame>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
Multiselect Checkbox checked event
private void MultiSelectCheckBox_CheckedChanged(object sender, CheckedChangedEventArgs e)
{
if (!e.Value)
{
foreach (MyData TS in MyObject)
{
TS.Selected = false;
}
}
else{
foreach (MyData TS in MyObject)
{
TS.Selected = true;
}
PerformSomeAction();
}
}
Single selection Checkbox changed event
private void Single_CheckedChanged(object sender, CheckedChangedEventArgs e)
{
if (!e.Value)
{
}
else{
PerformSomeAction();
}
}
Data Model
public class MyData : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public string name { get; set; }
private bool selected;
public bool Selected
{
get
{
return selected;
}
set
{
if (value != null)
{
selected = value;
NotifyPropertyChanged("Selected");
}
}
}
}
Agree with # Nikhileshwar , you could define some properties to get the different condition .And since you had used MVVM, you would better put all logic handling in your viewmodel .
in xaml
<Grid >
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<StackLayout Grid.Row="0" Orientation="Horizontal" HeightRequest="40" BackgroundColor="LightPink">
<Label Text="Select All" FontSize="Micro" TextColor="Red" HorizontalOptions="Start" VerticalOptions="Center" >
</Label>
<CheckBox x:Name="MultiselectCheckbox" ScaleX="0.8" ScaleY="0.8" IsChecked="{Binding MultiselectCheck}" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Color="Red"></CheckBox>
</StackLayout>
<ListView Grid.Row="1"
x:Name="Listview"
HorizontalOptions="FillAndExpand"
ItemsSource="{Binding MyItems}"
VerticalOptions="FillAndExpand" >
<ListView.ItemTemplate >
<DataTemplate >
<ViewCell >
<Frame Padding="0" HeightRequest="40" HorizontalOptions="FillAndExpand">
<StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand">
<Label Text="{Binding name}" FontSize="Micro" HorizontalOptions="StartAndExpand" VerticalOptions="Center" TextColor="Red" Margin="5,0,0,0">
</Label>
<CheckBox IsChecked="{Binding Selected}" Color="Red" HorizontalOptions="End" >
</CheckBox>
</StackLayout>
</Frame>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
in ViewModel
public class MyViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
bool isMultiselect;
bool isSingleSelect;
public ObservableCollection<MyData> MyItems { get; set; }
private bool multiselectCheck;
public bool MultiselectCheck
{
get
{
return multiselectCheck;
}
set
{
if (multiselectCheck != value)
{
multiselectCheck = value;
if(!isSingleSelect)
{
isMultiselect = true;
foreach (MyData data in MyItems)
{
data.Selected = value;
}
isMultiselect = false;
}
NotifyPropertyChanged("MultiselectCheck");
}
}
}
public MyViewModel()
{
MyItems = new ObservableCollection<MyData>() {
new MyData(){name="Selection1" },
new MyData(){name="Selection2" },
new MyData(){name="Selection3" },
};
foreach(MyData data in MyItems)
{
data.PropertyChanged += Data_PropertyChanged;
}
}

How do we bind data to a `footer` in Xamarin Forms

How do we bind data to a footer inside ListView in Xamarin Forms, here I would need to pass the count_in value to footer.
<ListView x:Name="listView">
<ListView.Footer>
<StackLayout>
<Label Text="{Binding Count}" BackgroundColor="Gray"></Label>
</StackLayout>
</ListView.Footer>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="2*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="2*"/>
</Grid.ColumnDefinitions>
<Image Source="{Binding image}" WidthRequest="50" HeightRequest="50" Grid.Column="0" VerticalOptions="Center"/>
<StackLayout Grid.Column="1">
<Label Text="{Binding FullName}" TextColor="#f35e20" HorizontalTextAlignment="Center"/>
</StackLayout>
<StackLayout Grid.Column="2">
<Label Text="{Binding SoccerStatus}" HorizontalTextAlignment="Center" TextColor="#503026"/>
</StackLayout>
<StackLayout Grid.Column="3">
<Label Text="{Binding CurrentDate}" HorizontalTextAlignment="Center" TextColor="#503026"/>
</StackLayout>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Below is the DisplayCount() gets the count from database;
public void DisplayCount()
{
var datetoday = DateTime.Now.ToString("ddMMyyyy");
var count_in = (from x in conn.Table<SoccerAvailability>().Where(x => string.Equals(x.SoccerStatus, "IN", StringComparison.OrdinalIgnoreCase) && x.CurrentDate == datetoday) select x).Count();
}
you are binding to Count
<Label Text="{Binding Count}" BackgroundColor="Gray"></Label>
so Count needs to be a public property on your ViewModel
public int Count { get; set; }
Now getting database exception SQLite exception no such function
equals
This is because SQLLite linq doesn't recognize the string.Equals method. You could convert it to list using ToListAsync for one condition. Then filter the c# list object using equals:
var datetoday = DateTime.Now.ToString("ddMMyyyy");
var items = await conn.Table<SoccerAvailability>().Where(x => x.CurrentDate == datetoday).ToListAsync();
var finalsItems = items.Where(x => string.Equals(x.SoccerStatus, "IN", StringComparison.OrdinalIgnoreCase)).ToList();
Count = finalsItems.Count();
At last, binding your Lable's Text to this Count property.
Edit about binding:
Have you set your content page to your ViewModel? Moreover, implement the INotifyPropertyChanged interface in your view model:
// Set your page's binding context
BindingContext = new PageViewModel();
public class PageViewModel : INotifyPropertyChanged
{
int count;
public int Count
{
set
{
if (count != value)
{
count = value;
onPropertyChanged();
}
}
get
{
return count;
}
}
public event PropertyChangedEventHandler PropertyChanged;
void onPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
Edit2:
If you didn't use view model, set a name to your footer label:
<ListView.Footer>
<StackLayout>
<Label x:Name="FooterLabel" BackgroundColor="Gray"></Label>
</StackLayout>
</ListView.Footer>
Then set its value directly:
//...
FooterLabel.Text = finalsItems.Count().ToString();
You need to specify the binding context for the footer.
By default for footers, headers, and templates in a list view, its binding context is going to be the item currently being displayed. So in this case Count would need to be a public property on the item from ItemsSource. Point the Count binding at your view model instead.
<Label Text="{Binding BindingContext.Count, Source={x:Reference xNameSetForCurrentPage}" BackgroundColor="Gray"></Label>
xNameSetForCurrentPage is the x:Name="name" set at the top most element(where all the xmlns stuff is) of the page.

How to update the rowheight of a clicked row in a listview in Xamarin forms?

This is my current listview XAML that works and i successfully fill my listview with data. I try to adjust the rowheight of a unique cell by both making an item in a stacklayout visible/not visible, but also by adjusting the heightrequest of both the stacklayout and grid.
<ContentPage.Resources>
<ResourceDictionary>
<DataTemplate
x:Key="ClassTemplate">
<ViewCell> <!-- I've tried to bind viewchells height (Height = "{Binding RowHeight}") but its not allowed to bind it -->
<StackLayout HeightRequest = "{Binding RowHeight}" HorizontalOptions = "FillAndExpand" VerticalOptions = "FillAndExpand">
<Button HeightRequest = "10" />
<Button IsVisible = "{Binding ExpandedRowVisibility}" HeightRequest = "300" />
<Grid VerticalOptions="Fill" HeightRequest = "{Binding RowHeight}">
<Grid.GestureRecognizers>
<TapGestureRecognizer
Command="{Binding OpenRowDetails}"
CommandParameter="{Binding .}"
NumberOfTapsRequired="1" />
</Grid.GestureRecognizers>
</Grid>
</StackLayout>
</ViewCell>
</DataTemplate>
</ResourceDictionary>
</ContentPage.Resources>
<ListView
ItemsSource="{Binding List}"
HasUnevenRows="true"
Header="{Binding ShowHeaderObject}"
ItemTemplate="{StaticResource ClassTemplate}">
<ListView.HeaderTemplate>
<DataTemplate>
<StackLayout>
<Label
Text="{Binding .}"
HorizontalTextAlignment="Center"
TextColor="White" />
</StackLayout>
</DataTemplate>
</ListView.HeaderTemplate>
</ListView>
When I click a row in the list I update my RowHeight value that is binded to the Stacklayout, grid and button (i've tried all three, alone and together) but the row i click on does not update.
private string Height = "140";
public string RowHeight
{
get { return Height; }
set
{
private bool ExpandRow;
public bool ExpandedRowVisibility
{
get { return ExpandRow; }
set
{
this.ExpandRow = value;
OnPropertyChanged("ExpandedRowVisibility");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged([CallerMemberName]string propertyName = null)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public ICommand OpenRowDetails
{
get
{
return new Command(e =>
{
var Item = (ClassItemViewModel)e;
Item.RowHeight = "280";
Item.ExpandedRowVisibility = true;
});
}
}
How can i update my code so that the row that was clicked will adjust its rowheight but the others stays the same?
Try this
<ListView x:Name="listView" HasUnevenRows="true">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout HorizontalOptions = "FillAndExpand" VerticalOptions = "FillAndExpand">
<StackLayout.GestureRecognizers>
<TapGestureRecognizer Tapped="OnItemTapped" NumberOfTapsRequired="1">
</StackLayout.GestureRecognizers>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
And in your tap event method change the height of the layout and update cell
void OnItemTapped(object sender, EventArgs args)
{
var layout = sender as StackLayout;
var viewCell = layout.Parent as ViewCell;
layout.HeightRequest = layout.Height + 100;
viewCell.ForceUpdateSize ();
}

Group Swtich Xamarin forms

I have a Listview that is creating dynamically data and a switch in every row, what i want is when i toggle a switch the others untoggle.
Is this possible to do?
Example:
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="6*"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Label Text="{Binding article_description}"
FontAttributes="Bold" FontSize="13" Margin="10,5,0,-6" Grid.Row="0" LineBreakMode="NoWrap"/>
<Label Text="{Binding dish_name}"
FontSize="13" Margin="10,0,0,2" Grid.Row="1" Grid.Column="0"/>
<Label Grid.Row="0" Grid.Column="0" x:Name="LabelReserved" Text="{Binding reserved}" IsVisible="false" LineBreakMode="NoWrap"/>
<Switch Grid.Row="0" Grid.RowSpan="2" Grid.Column="1" HorizontalOptions="Start" VerticalOptions="Center" IsEnabled="False" Toggled="SwitchMenu_OnToggled" >
<Switch.Triggers>
<DataTrigger TargetType="Switch" Binding="{Binding Source={x:Reference LabelReserved},
Path=Text.Length}" Value="7">
<Setter Property="IsToggled" Value="true" />
</DataTrigger>
</Switch.Triggers>
</Switch>
</Grid>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
Note: This solution was a bit of an experiment on my part - so I would recommend that, if you decide to implement this, use it with caution.
The intent here is to extend Switch to be able to act as a grouped radio button.
First step would be to create a IsToggled or IsChecked or similar property in the item that acts as BindingContext to each list-item. You can implement an interface like:
public interface IToggableItem
{
string GroupName { get; } //not mandatory, only added to support grouped lists
bool IsChecked { get; set; }
}
Second step would be extend Switch to be aware of items-list. We can do that by adding a GroupContext bindable property - which basically represents the parent list-view's ItemsSource.
When a switch is toggled, it iterates through the items-list to set the property as false on other items.
For example:
public class GroupedSwitch : CustomSwitch
{
public static readonly BindableProperty IsGroupingEnabledProperty =
BindableProperty.Create(
"IsGroupingEnabled", typeof(bool), typeof(GroupedSwitch),
defaultValue: default(bool));
public bool IsGroupingEnabled
{
get { return (bool)GetValue(IsGroupingEnabledProperty); }
set { SetValue(IsGroupingEnabledProperty, value); }
}
public static readonly BindableProperty GroupContextProperty =
BindableProperty.Create(
"GroupContext", typeof(IEnumerable), typeof(GroupedSwitch),
defaultValue: default(IEnumerable));
public IEnumerable GroupContext
{
get { return (IEnumerable)GetValue(GroupContextProperty); }
set { SetValue(GroupContextProperty, value); }
}
protected override void OnPropertyChanged(string propertyName = null)
{
base.OnPropertyChanged(propertyName);
if (propertyName != nameof(IsToggled))
return;
if (IsToggled != true || GroupContext == null)
return;
var currentItem = BindingContext as IToggableItem;
if (currentItem == null)
return;
if (IsGroupingEnabled)
{
var groupList = GroupContext as IEnumerable<IGrouping<string, IToggableItem>>;
var currentGroup = groupList.FirstOrDefault(x => x.Key == currentItem.GroupName);
if (currentGroup != null)
foreach (var item in currentGroup)
{
if (item != currentItem)
item.IsChecked = false;
}
}
else
{
var simpleList = GroupContext as IEnumerable<IToggableItem>;
if (simpleList != null)
foreach (var item in simpleList)
{
if (item != currentItem)
item.IsChecked = false;
}
}
}
}
Third step would be bind GroupContext property to parent ListView's items source. For example:
<ListView x:Name="ParentListView" >
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal" VerticalOptions="Center">
<Label Text="{Binding Name}" />
<local:GroupedSwitch
IsToggled="{Binding IsChecked}"
GroupContext="{Binding ItemsSource, Source={x:Reference ParentListView}}" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Or,
<ListView x:Name="_parentList" IsGroupingEnabled="true" >
<ListView.GroupHeaderTemplate>
<DataTemplate>
<TextCell Text="{Binding Key}" />
</DataTemplate>
</ListView.GroupHeaderTemplate>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal">
<Label Text="{Binding Name}" />
<local:GroupedSwitch
ToggledStateFromCode="{Binding IsSwitchOn}"
IsGroupingEnabled="true"
GroupContext="{Binding ItemsSource, Source={x:Reference _parentList}}" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
EDIT 1: Updated code to add support for grouped list.
You should use triggers on each switch. Check this out:
https://www.tutorialspoint.com/xaml/xaml_triggers.htm

Resources