Restore Previous selection of RadComboBox

3 posts, 0 answers
  1. Javor
    Javor avatar
    25 posts
    Member since:
    Sep 2013

    Posted 27 Mar Link to this post

    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. Javor
    Javor avatar
    25 posts
    Member since:
    Sep 2013

    Posted 27 Mar Link to this post

    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;
                    }
                }
            }

     

  3. Martin Ivanov
    Admin
    Martin Ivanov avatar
    2135 posts

    Posted 01 Apr Link to this post

    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.
Back to Top