How to get sfPicker value ?

Hello,

How can I get the selected value from the sfpicker and display it in a label?

Best regards,

Stjepan Jurić


1 Reply

IR Indumathi Ravichandran Syncfusion Team March 13, 2024 06:19 AM UTC

Hi Stjepan,


Regarding Query: How can I get the selected value from the sfpicker and display it in a label?


As per the shared information, we suspect your requirement is “Getting the selected item value from the picker in the .NET MAUI Picker and show it in label”. By using the SelectionChanged event of the picker you can achieve this requirement in sample level. We have attached the code snippet for the same.


Code snippet:

<StackLayout>

    <Label x:Name="label" Text="Picker" VerticalOptions="Center" HorizontalOptions="Center" BackgroundColor="Coral"/>

    <picker:SfPicker x:Name="picker" Mode="Dialog" IsOpen="True" SelectionChanged="picker_SelectionChanged">

        <picker:SfPicker.FooterView>

            <picker:PickerFooterView Height="40"/>

        </picker:SfPicker.FooterView>

    </picker:SfPicker>

</StackLayout>

 

 

private void picker_SelectionChanged(object sender, PickerSelectionChangedEventArgs e)

{

    ObservableCollection<string> dataSource = new ObservableCollection<string>();

 

    if (this.picker.Columns != null && this.picker.Columns[0].ItemsSource is ICollection collection)

    {

        if (collection.OfType<string>().Any())

        {

            foreach (string item in collection)

            {

                dataSource.Add(item);

            }

        }

    }

 

   

    label.Text = dataSource[this.picker.Columns![0].SelectedIndex];

}


Screenshot:



We hope that this helps you. Please let us know if you need further assistance.


Regards,

Indumathi R


Loader.
Up arrow icon