This is a migrated thread and some comments may be shown as answers.

Restore Previous selection of RadComboBox

2 Answers 342 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Javor
Top achievements
Rank 1
Javor asked on 27 Mar 2019, 05:34 PM

It is possible to restore selected value in the view model i'm trying 
something like this but combobox do not changed to the correct item you see last selected from the use 

<telerik:RadComboBox ItemsSource="{Binding Tests}" Width="150" Height="25"
                             SelectedValuePath="Id">
            <telerik:RadComboBox.SelectedValue>
                <Binding Path="SelectedTestId" Mode="TwoWay" BindsDirectlyToSource="True" UpdateSourceTrigger="PropertyChanged"/>
            </telerik:RadComboBox.SelectedValue>
            <telerik:RadComboBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Name}"/>
                </DataTemplate>
            </telerik:RadComboBox.ItemTemplate>
        </telerik:RadComboBox>

     public int SelectedTestId
        {
            get
            {
                return selectedTestId;
            }
            set
            {
                OldSelectionTestId = selectedTestId;

                SetProperty(ref selectedTestId, value);

                if (OldSelectionTestId > 20 && SelectedTestId == 20)
                {
                    Debug.WriteLine($"oldSelectionTestId = {OldSelectionTestId}");
                    Debug.WriteLine($"SelectedTestId = {SelectedTestId}");
                    HasError = true;
                }
            }
        }

2 Answers, 1 is accepted

Sort by
0
Javor
Top achievements
Rank 1
answered on 27 Mar 2019, 05:38 PM

The C# code is wrong  here is the correct
  public int SelectedTestId
        {
            get
            {
                return selectedTestId;
            }
            set
            {
                OldSelectionTestId = selectedTestId;

                SetProperty(ref selectedTestId, value);

                if (OldSelectionTestId > 20 && SelectedTestId == 20)
                {
                    Debug.WriteLine($"oldSelectionTestId = {OldSelectionTestId}");
                    Debug.WriteLine($"SelectedTestId = {SelectedTestId}");
                    SelectedTestId = OldSelectionTestId;
                }
            }
        }

 

0
Martin Ivanov
Telerik team
answered on 01 Apr 2019, 11:01 AM
Hello Javor,

Yes, you can restore the selection from the view model.

I've tried to prepare a sample that shows how to do this, but I am not sure when you want to restore the selection. If you clear this our I will think of a possible approach. Anyway, the basic idea here would be to set the SelectedTestId when necessary (as you are already doing).

I also attached my test project, if this could be somehow helpful.

Regards,
Martin Ivanov
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Paul
Top achievements
Rank 2
Iron
Iron
Iron
commented on 08 Dec 2023, 12:43 AM

I tried this but the item selected remains on screen.
It goes through the setter code again but the display does not show the previous item
e,g. Select Item 3 - "don't want to let user select Item 3 at this time" - set property back to 1, Item 3 is still showing on screen.

Martin Ivanov
Telerik team
commented on 11 Dec 2023, 09:16 AM

To achieve your requirement, you can use the SelectedItem property of RadComboBox. You can see this suggestion shown in the attached project. I hope it helps.

Tags
ComboBox
Asked by
Javor
Top achievements
Rank 1
Answers by
Javor
Top achievements
Rank 1
Martin Ivanov
Telerik team
Share this question
or