ComboBox not showing selected item

2 posts, 0 answers
  1. Kelmen
    Kelmen avatar
    1 posts
    Member since:
    Jan 2012

    Posted 13 Jan 2012 Link to this post

    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; }
    }
  2. Yana
    Admin
    Yana avatar
    4554 posts

    Posted 13 Jan 2012 Link to this post

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

  3. DevCraft banner
Back to Top