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

SelectedItem after setting ItemsSource

3 Answers 631 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
NS
Top achievements
Rank 1
NS asked on 17 Oct 2008, 07:13 AM
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

3 Answers, 1 is accepted

Sort by
0
Valeri Hristov
Telerik team
answered on 17 Oct 2008, 08:52 AM

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.
0
bill
Top achievements
Rank 1
answered on 10 Dec 2008, 06:45 AM

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?





0
Valeri Hristov
Telerik team
answered on 12 Dec 2008, 12:23 PM
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.
Tags
ComboBox
Asked by
NS
Top achievements
Rank 1
Answers by
Valeri Hristov
Telerik team
bill
Top achievements
Rank 1
Share this question
or