ComboBox Issues

2 posts, 0 answers
  1. Marco Teodoro
    Marco Teodoro avatar
    53 posts
    Member since:
    Apr 2010

    Posted 22 Mar 2011 Link to this post

    Hi,

    I am posting this thread because i am getting some issues in the binding of the combobox. I've even followed your examples and demos, and it wont work.
    Let's list the problems:

    •  First the items won't show up
    •  And i wanted to make one specific item (for each client i.e) to appear, so i am not speaking about SelectedIndex


    Here is a sample code of this issues :
    namespace SilverlightApplication1
    {
        public partial class Page1 : Page, INotifyPropertyChanged
        {
            public Page1()
            {
                InitializeComponent();
            }
     
            private ObservableCollection<test> _list = new ObservableCollection<test>();
     
            public ObservableCollection<test> List
            {
                get
                {
                    return _list;
                }
                set
                {
                    if (_list == value) return;
                    _list = value;
     
                    LauchNotification("List");
     
                }
            }
     
            private test _test = null;
     
            public test Test
            {
                get
                {
                    return _test;
                }
                set
                {
                    if (_test == value) return;
                    _test = value;
     
                    LauchNotification("Test");
     
                }
            }
     
            private void Page_Loaded(object sender, RoutedEventArgs e)
            {
                MessageBox.Show("test");
                _list = new ObservableCollection<test>();
     
                test test2 = new test();
                test test3 = new test();
     
                Test.Description = "Item1";
                Test.Id = 1;
     
                test2.Description = "item2";
                test2.Id = 2;
                test3.Description = "item3";
                test3.Id = 3;
     
                _list.Add(Test);
                _list.Add(test2);
                _list.Add(test3);
            }
     
            public event PropertyChangedEventHandler PropertyChanged;
     
            private void LauchNotification(string propName)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(propName));
                }
            }
        }
     
        public class test : INotifyPropertyChanged
        {
            private string _description = string.Empty;
     
            public string Description
            {
                get
                {
                    return _description;
                }
                set
                {
                   if  (_description == value) return;
                    _description = value;
     
                    LauchNotification("Description");
     
                }
            }
             
            private int _id = 0;
     
            public int Id
            {
                get
                {
                    return _id;
                }
                set
                {
                    if (_id == value) return;
                    _id = value;
     
                    LauchNotification("Id");
     
                }
            }
     
     
            public event PropertyChangedEventHandler PropertyChanged;
     
            private void LauchNotification(string propName)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(propName));
                }
            }
        }
    }

    and now the XAML:
    <navigation:Page x:Class="SilverlightApplication1.Page1"
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
               mc:Ignorable="d"
               xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
               d:DesignWidth="640" d:DesignHeight="480"
               Title="Page1 Page" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" Loaded="Page_Loaded">
        
        <Grid x:Name="LayoutRoot">
            <ComboBox Height="23" Name="comboBox1" VerticalAlignment="Top" Margin="160,148,0,0" 
                      ItemsSource="{Binding List}"
                      SelectedItem="{Binding Test, Mode=TwoWay}"
                      DisplayMemberPath="Description"/>
        </Grid>
    </navigation:Page>


    Thanks in advance for your help, Best Regards
  2. Valeri Hristov
    Admin
    Valeri Hristov avatar
    2252 posts

    Posted 23 Mar 2011 Link to this post

    Hello Marco,

    The items are not displayed because the page DataContext is not set properly and the bindings do not work (you can check the error messages in the Visual Studio output window). There are many ways to resolve the problem, a good one is to create a ViewModel class that contains the List, Test, etc. properties and set it as a DataContext on the Page.

    I am not sure that I understand your second question though. Do you want to select a combobox item depending on the value of one of its properties?

    Regards,
    Valeri Hristov
    the Telerik team
  3. DevCraft banner
Back to Top