RadListPicker - missed selected index property

1 Answer 184 Views
ListPicker
Daniel
Top achievements
Rank 1
Silver
Bronze
Daniel asked on 22 Mar 2022, 05:37 AM

Hi,

RadListPicker - missed selected index property

Thanks,

<telerikInput:RadListPicker  x:Name="timeTypeListPicker" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand" HeightRequest="35"  WidthRequest="100"
                          ItemsSource="{Binding TimeTypesItem}" 
                          DisplayMemberPath="Value" 
                          SelectedItem="{Binding SelectedTimeTypesItem, Mode=TwoWay}" >
                                    </telerikInput:RadListPicker>


Daniel
Top achievements
Rank 1
Silver
Bronze
commented on 22 Mar 2022, 02:25 PM

When you set in view model constructor the selected item the selection in RadListPicker is empty is not working in window.
Lance | Senior Manager Technical Support
Telerik team
commented on 22 Mar 2022, 06:12 PM

Works for me:

Using the following code.

MainPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="GeneralFeatures.MainPage"
             xmlns:telerikInput="clr-namespace:Telerik.XamarinForms.Input;assembly=Telerik.Maui.Controls.Compatibility"
             BackgroundColor="{DynamicResource SecondaryColor}">

    <Grid>
        <telerikInput:RadListPicker  x:Name="timeTypeListPicker"
                                     VerticalOptions="Start"
                                     ItemsSource="{Binding Items}"
                                     DisplayMemberPath="FullName"
                                     SelectedItem="{Binding SelectedItem, Mode=TwoWay}" />
        
        <Label Text="{Binding StatusText}" 
               VerticalOptions="End"
               HorizontalOptions="Center"
               Margin="10"/>
    </Grid>
</ContentPage>

 

MainPage.xaml.cs

 

using System.Collections.ObjectModel;
using Telerik.XamarinForms.Common;

namespace GeneralFeatures;

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
        BindingContext = new PeopleViewModel();
    }
}

public class PeopleViewModel : NotifyPropertyChangedBase
{
    public Person selectedItem;
    private string statusText;

    public PeopleViewModel()
    {
        Items = new ObservableCollection<Person>()
        {
            new Person("Freda","Curtis"),
            new Person("Jeffery","Francis"),
            new Person("Ema","Lawson"),
            new Person("Niki","Samaniego"),
            new Person("Jenny","Santos"),
            new Person("Eric","Wheeler"),
            new Person("Emmett","Fuller"),
            new Person("Brian","Johnas"),
        };

        SelectedItem = Items[3];
    }

    public ObservableCollection<Person> Items { get; set; }

    public Person SelectedItem
    {
        get => selectedItem;
        set
        {
            if (UpdateValue(ref selectedItem, value))
            {
                var selectedIndex = Items.IndexOf(selectedItem);
                StatusText = $"Selected Index: {selectedIndex}";
            }
        }
    }

    public string StatusText 
    { 
        get => statusText; 
        set => UpdateValue(ref statusText, value); 
    }
}

public class Person
{
    public Person(string name, string lastName)
    {
        Name = name;
        LastName = lastName;
    }

    public string Name { get; set; }

    public string LastName { get; set; }

    public string FullName => $"{Name} {LastName}";
}

Moving Forward

Here are some ideas you can ponder while investigating your custom business logic and application lifecycle:

  • You're using a non-null value for the selected value (i.e. use an item form the ItemsSource)
  • You're calling OnPropertyChanged for the SelectedItem property.
  • The logic is being performed in the correct order (e.g., the selected value must be set after the ItemsSource is set)
  • Use a breakpoint in your property setters to make sure the value you are getting is the value you are expecting.
  • Use an extra "StatusText" property like I did in my example. This will help you understand what is going on.

1 Answer, 1 is accepted

Sort by
0
Lance | Senior Manager Technical Support
Telerik team
answered on 22 Mar 2022, 01:46 PM

Hi Daniel,

This is by design, because there is a SelectedItem property. We have found, through developer demand, that it is more desired and more functional than SelectedIndex (because it supports type of Object and not just int).

In any case, I have opened a Feature Request on your behalf => Provide SelectedIndex Property for ListPicker (telerik.com). I have already added your upvote, so there is no further action needed by you.

We will track the demand using that feedback item and will update the Status as needed.

 

Side Note

If you really need an int index instead of an object reference, you can use SelectedItem and use IndexOf method:

public ObservableCollection<Thing> Items { get; set; }

public Thing selectedItem;
public Thing SelectedItem 
{
    get => selectedItem;
    set
    {
        if (value == selectedItem)
            return;

        selectedItem = value;
        OnPropertyChanged();

        // Selected Index
        var selectedIndex = Items.IndexOf(selectedItem);
    }
}

Regards,


Lance | Manager Technical Support
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
ListPicker
Asked by
Daniel
Top achievements
Rank 1
Silver
Bronze
Answers by
Lance | Senior Manager Technical Support
Telerik team
Share this question
or