Updating SfPicker Value Programmatically

I am trying to update SfPicker value programmatically like this:

picker.SelectedIndex = newValue;

It does get selected but the selected value is not visible on the control, i.e. the control is not updated/redrawn.
What is the right way of doing it programmatically?

Thanks,
Vassili


4 Replies

SP Sakthivel Palaniyappan Syncfusion Team March 5, 2020 03:25 PM UTC

Hi Vassili,

Greetings from Syncfusion.

We have analyzed your query and checked the reported issue of “SelectedIndex not updating programmatically”, but we are unable to reproduce the reported issue. Please find the sample from below

Sample link:
https://www.syncfusion.com/downloads/support/directtrac/general/ze/GettingStarted-1828834282.zip

Since we do not know the exact scenario of your case, can you update complete code snippet or
modified sample with the reported issue. Which will be helpful for us to investigate more about and
provide better solution at the earliest.

Regards,
Sakthivel P.




VA Vassili March 5, 2020 04:56 PM UTC

Yes, your sample runs without a problem indeed. But I now see what the difference is: you have only text, but I have both, text and images.
I explicitly build each Picker entry (row) in code as follows (m_pics is a string list containing all the images names and m_strings is a string list that contains all the text entries - current image is m_pics[row] and current row text is m_strings[row]):

        void LoadPickerView(int row)
        {
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
              ViewGroup.LayoutParams.WrapContent,
              ViewGroup.LayoutParams.WrapContent
            );
            layoutParams.Height = (int)m_picker.ItemHeight;
            layoutParams.Width = (int)m_picker.Width;
            LinearLayout rowView = new LinearLayout(m_context) { Orientation = Orientation.Vertical };

            LinearLayout rowData = new LinearLayout(m_context);
            rowData.Orientation = Orientation.Horizontal;

            ImageView rowImage = null;
            if (m_pics != null && m_pics.Count > row)
            {
                rowImage = new ImageView(m_context);
                rowImage.SetImageResource(m_pics[row]);
                AdjustPickerView(rowImage, row);
                rowData.AddView(rowImage, ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
            }
            TextView rowText = null;
            if (m_strings != null && m_strings.Count > row)
            {
                rowText = new TextView(m_context);
                rowText.Text = " " + m_strings[row];
                rowText.TextAlignment = m_alignment.Item2;
                rowText.SetBackgroundColor(m_bgColor);
                rowText.Gravity = GravityFlags.CenterVertical;
                AdjustPickerView(rowText, row);
                rowData.AddView(rowText, ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
            }

            ImageView line = new ImageView(m_context);
            line.SetImageResource(m_linePic);
            line.LayoutParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.WrapContent);

            rowView.AddView(rowData, ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.WrapContent);
            rowView.AddView(line, ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.WrapContent);
            rowView.LayoutParameters = layoutParams;

            m_imageViews.Add(rowImage);
            m_textViews.Add(rowText);
            m_pickerViews.Add(rowView);
        }

Then I assign all these Views in the OnPickerItemLoaded  event handler:

            m_picker.OnPickerItemLoaded += (sender, e) =>
            {
                if (m_pics == null)
                {
                    return;
                }
                LoadPickerViewsIfNeeded();
                e.CustomView = m_pickerViews[e.Row];
            };

So when I have no images (m_pics = null) all is fine. With Images it doesn't change the picker wheel on a programmatic select (but the entry is selected, just not visible).



VA Vassili March 5, 2020 05:06 PM UTC

Actually I realized that I was missing this statement:

            m_picker.IsOpen = true;

It started working after I added it (got it from your sample, thanks).
Somehow I thought it was only used for a dialog picker.

Thanks for your support!
Vassili


SP Sakthivel Palaniyappan Syncfusion Team March 6, 2020 05:24 AM UTC

Hi Vassili,

Thanks for the update.

We are glad to know that the reported issue gets resolved.

Please let us know if you have any further assistance.

Regards,
Sakthivel P.


Loader.
Up arrow icon