Why does value of picker disappear while listview is tapped or scrolled?

Viewed 66

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!

1 Answers

I am not sure what is your CustomCell. But when we define the Data Templates, we normally use ViewCell, ContentView.

When use DataTemplateSelector, we follow the steps below.

  1. Creating a DataTemplateSelector.
  2. Consuming a DataTemplateSelector in XAML.
  3. Assign it to the ItemTemplate property.

You could refer to the code i done before. Load ContentView inside CarouselView in Xamarn forms

You consume the PickerTemplate to DataTemplateSelector in Xaml and you also do this in CustomCellSelector contructure. Remove it in your selector.

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;
}
}

You have define the DataTemplate for Picker, Button, Entry, Swich in xaml. There is no need to do something like PickerCell and assign it in CustomCellSelector constructure.

Related