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

Restore Previous selection of RadComboBox

2 Answers 61 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.
Tags
ComboBox
Asked by
Javor
Top achievements
Rank 1
Answers by
Javor
Top achievements
Rank 1
Martin Ivanov
Telerik team
Share this question
or