I have Listview, that use DataTemplateSelector for dynamic adding of custom DataTemplates (containing picker). My issue sets when I scroll or tap listview. In this case are selected items of pickers set to null (there is no value in the row).
Here is part of my code...
xaml:
<DataTemplate x:Key="PickerTemplate">
<ViewCell Height="70">
<ViewCell.View>
<StackLayout Orientation="Vertical" Padding="5" >
<Label Text="{Binding name}" FontAttributes="Bold"/>
<StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand">
<local:myPicker x:Name="Picker_cell" ItemsSource="{Binding items}" SelectedIndex="{Binding selectedIndex}" TextColor="{Binding TextColor}" IsEnabled="{Binding enabled}"
HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" HorizontalTextAlignment="End" SelectedIndexChanged="Picker_cell_SelectedIndexChanged"
CommandParameter="{Binding commandParameter}" Focused="Picker_cell_Focused"/>
</StackLayout>
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
.
.
.
<local:CustomCellSelector x:Key="CustomCellSelector"
PickerTemplate="{StaticResource PickerTemplate}"
ButtonTemplate="{StaticResource ButtonTemplate}"
EntryTemplate="{StaticResource EntryTemplate}"
SwitchTemplate="{StaticResource SwitchTemplate}"/>
.
.
.
<ListView x:Name="mainlistView" ItemTemplate="{StaticResource CustomCellSelector}" SelectionMode="None" SeparatorColor="Black" HasUnevenRows="True" CachingStrategy="RecycleElement"
ItemsSource="{Binding CustomCells}" Grid.Row="0" Grid.Column="0"/>
DataTemplateSelector:
public class CustomCellSelector : DataTemplateSelector
{
public DataTemplate PickerTemplate { get; set; }
public DataTemplate ButtonTemplate { get; set; }
public DataTemplate EntryTemplate { get; set; }
public DataTemplate SwitchTemplate { get; set; }
public CustomCellSelector()
{
this.PickerTemplate = new DataTemplate(typeof(PickerCell));
this.ButtonTemplate = new DataTemplate(typeof(ButtonCell));
this.EntryTemplate = new DataTemplate(typeof(EntryCell));
this.SwitchTemplate = new DataTemplate(typeof(SwitchCell));
}
protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
{
if (item is PickerCell) return PickerTemplate;
if (item is ButtonCell) return ButtonTemplate;
if (item is EntryCell) return EntryTemplate;
return SwitchTemplate;
}
}
ViewModel of Picker:
public class PickerCell : CustomCell
{
.
.
.
private int _selectedIndex;
public int selectedIndex
{
get
{
return _selectedIndex;
}
set
{
if (_selectedIndex != value)
{
_selectedIndex = value;
OnPropertyChanged(nameof(selectedIndex));
}
}
}
.
.
.
}
Does anyone know how to fix this issue?
Thank you for your response!
If value of picker is undesirable value (selectedIndex = -1, selectedIndex = null...), then don't change it.
Edited ViewModel of Picker:
public class PickerCell : CustomCell
{
.
.
.
private int _selectedIndex;
public int selectedIndex
{
get
{
return _selectedIndex;
}
set
{
if (_selectedIndex != value)
{
_selectedIndex = value;
if(_selectedIndex != -1) OnPropertyChanged(nameof(selectedIndex));
}
}
}
.
.
.
}