SelectedItem after setting ItemsSource

4 posts, 0 answers
  1. NS
    NS avatar
    189 posts
    Member since:
    Jan 2007

    Posted 17 Oct 2008 Link to this post

    Hi,

    Consider the following code:
    myType filledVar {get;set;}

    List<myType> myList = new List();

    combobox1.ItemsSource = myList

    How can you after set the ItemsSource set the selectedIndex to a value that is present in filledVar ?

    e.g. user has a property language. You bind a combobox with languages. How can you set the language in the combobox to the language of the user?

    In the asp.net suite you had something like: FindItemByValue of FindItemByText if I remember correctly.

    Thanks,
    Nicolas
  2. Valeri Hristov
    Admin
    Valeri Hristov avatar
    2252 posts

    Posted 17 Oct 2008 Link to this post

    Hi NS,

    In RadControls for Silverlight is a little bit different:

    <telerikInput:RadComboBox x:Name="Combo1"></telerikInput:RadComboBox>

    public partial class Page : UserControl
    {
     public Page()
     {
      InitializeComponent();

      Combo1.ItemsSource = new List<DataItem>()
      {
       new DataItem() { Text="Item1", Value=1 },
       new DataItem() { Text="Item2", Value=2 },
       new DataItem() { Text="Item3", Value=3 }
      };
      Combo1.DisplayMemberPath = "Text";
      Combo1.SelectedValuePath = "Value";
      Combo1.SelectedValue = 2;
     }
    }

    public class DataItem
    {
     public string Text { get; set; }
     public int Value { get; set; }
    }

    Of course, you can set or bind the SelectedValue, SelectedValuePath and DisplayMemberPath in XAML.

    Kind regards,

    Valeri Hristov
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. bill
    bill avatar
    14 posts
    Member since:
    Sep 2008

    Posted 10 Dec 2008 Link to this post

    Please see my question at the end of this code snippet that you posted:


    Hi NS,

    In RadControls for Silverlight is a little bit different:

    <telerikInput:RadComboBox x:Name="Combo1"></telerikInput:RadComboBox>

    public partial class Page : UserControl
    {
     public Page()
     {
      InitializeComponent();

      Combo1.ItemsSource = new List<DataItem>()
      {
       new DataItem() { Text="Item1", Value=1 },
       new DataItem() { Text="Item2", Value=2 },
       new DataItem() { Text="Item3", Value=3 }
      };
      Combo1.DisplayMemberPath = "Text";
      Combo1.SelectedValuePath = "Value";
      Combo1.SelectedValue = 2;
     }
    }

    public class DataItem
    {
     public string Text { get; set; }
     public int Value { get; set; }
    }


    In the above example why is it that later on in the code if you do something like:

    DataItem d1 = Combo1.SelectedItem;

    SelectedItem returns "null" ... Even though  its displaying "item2" as the selected item

    Also a call like:

    int x = Combo1.SelectedIndex;

    SelectedIndex returns -1 ... Again even though visually "item2" is displaying as the selected item.

    But it seems to only work when you make the call using selected value like:

    int z = Combo1.SelectedValue;

    This works correctly and returns "2".

    Is this a bug or is this somehow a wierd way of how this is suppose to work?





  4. Valeri Hristov
    Admin
    Valeri Hristov avatar
    2252 posts

    Posted 12 Dec 2008 Link to this post

    Hello bill,

    I was unable to reproduce the problem only with the attached application. I tried with several versions of RadControls for Silverlight, but still to no avail. Could you please open a new support ticket and send me a simple working application, including the Telerik assemblies, that could be used to observe the problem?

    All the best,
    Valeri Hristov
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Back to Top