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

ComboBox not showing selected item

1 Answer 94 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Kelmen
Top achievements
Rank 1
Kelmen asked on 13 Jan 2012, 11:03 AM

when I build a list of RadComboBoxItem and assign them to the RadComboBox...
1) it doesn't show the expected default selected item
2) when i select an item from the list, the box doesn't show the selected item

if I change this list of RadComboBoxItem to list of data object, it worked.

//cb2.DisplayMemberPath = "DataContext.Text";
//cb2.SelectedValuePath = "DataContext.Value";
 
cb2.DisplayMemberPath = "Text";
cb2.SelectedValuePath = "Value";
 
//Binding b = new Binding("Value");
//b.ElementName = "DataContext";
////b.Path.Path = "Value";
//cb2.SetBinding(Telerik.Windows.Controls.RadComboBox.SelectedValuePathProperty, b);

I tried the above codes, none worked. the above codes worked when the itemsources is list of data objects.

below are the full codes:

<StackPanel Orientation="Vertical">
    <telerik:RadComboBox Name="cb1" Width="200" />
 
    <telerik:RadComboBox Name="cb2" Width="200" Margin="5,20,5,5" />
</StackPanel>
public partial class MainPage : UserControl
{
    public MainPage()
    {
        InitializeComponent();
 
        BuildData();
 
        this.DataContext = MyDataInst;
 
        Build1();
        Build2();
 
        cb2.SelectionChanged += new Telerik.Windows.Controls.SelectionChangedEventHandler(cb2_SelectionChanged);
    }
 
    void cb2_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangedEventArgs e)
    {
         
    }
 
    List<MyData> MyDataList = new List<MyData>();
 
    MyData MyDataInst = new MyData
    {
        Value = "1",
        Text = "1",
    };
 
    void BuildData()
    {
        MyDataList.Add(new MyData { Value = null, Text = "null", });
        MyDataList.Add(MyDataInst);
        MyDataList.Add(new MyData { Value = "2", Text = "2", });
    }
 
    void Build1()
    {
        //cb1.SelectedValue
        //SetBinding(cb1, new System.Windows.Data.Binding("Value"));
        cb1.SetBinding(Telerik.Windows.Controls.RadComboBox.SelectedValueProperty, new System.Windows.Data.Binding("Value"));
         
 
        cb1.DisplayMemberPath = "Text";
        cb1.SelectedValuePath = "Value";
 
        this.cb1.ItemsSource = MyDataList;
    }
 
    void Build2()
    {
        cb2.SetBinding(Telerik.Windows.Controls.RadComboBox.SelectedValueProperty, new System.Windows.Data.Binding("Value"));
 
        //cb2.DisplayMemberPath = "DataContext.Text";
        //cb2.SelectedValuePath = "DataContext.Value";
 
        cb2.DisplayMemberPath = "Text";
        cb2.SelectedValuePath = "Value";
 
        //Binding b = new Binding("Value");
        //b.ElementName = "DataContext";
        ////b.Path.Path = "Value";
        //cb2.SetBinding(Telerik.Windows.Controls.RadComboBox.SelectedValuePathProperty, b);
 
        List<Telerik.Windows.Controls.RadComboBoxItem> dl = new List<Telerik.Windows.Controls.RadComboBoxItem>();
        foreach (var d in MyDataList)
        {
            Telerik.Windows.Controls.RadComboBoxItem cbi = new Telerik.Windows.Controls.RadComboBoxItem();
            cbi.DataContext = d;
            cbi.SetBinding(Telerik.Windows.Controls.RadComboBoxItem.ContentProperty, new Binding("Text"));
            dl.Add(cbi);
        }
 
        this.cb2.ItemsSource = dl;
    }
}
 
public class MyData
{
    public string Value { get; set; }
    public string Text { get; set; }
}

1 Answer, 1 is accepted

Sort by
0
Yana
Telerik team
answered on 13 Jan 2012, 05:16 PM
Hi Kelmen,

This is expected behavior - you cannot bind the ComboBox to a list of RadComboBoxItems. The combobox handles the visual and non-visual elements differently and the recommended approach is to bind it to a collection of objects.

Hope this helps.

Regards,
Yana
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
ComboBox
Asked by
Kelmen
Top achievements
Rank 1
Answers by
Yana
Telerik team
Share this question
or