Binding issue

2 posts, 0 answers
  1. Greg Cockerham
    Greg Cockerham avatar
    1 posts
    Member since:
    Jan 2010

    Posted 14 Dec 2010 Link to this post

    I have a set of 4 combo boxes with their ItemSource bound to an ObservableCollection of ProjectResource objects(these are in essence just person objects with fields FullName, FirstName, Email, Phone, etc..)  I have the DisplayMemberPath of each combo box set to the FullName field in the ProjectResource class.  This works as expected.  Each combo box has its SelectedItem property bound(two-way) to a different ProjectResource property in the ViewModel.  The problem I am seeing is although I set the value of each of the underlying properties(in this case the ProductManager property), when the page containing the combo box is loaded, the combo box displays the EmptyText rather than the FullName of the ProductManager property.  This was working during some prototyping, when the ProductManager property was just a string.  Once I changed it to type ProjectResource it stopped working.  Any ideas?

    Here is the XAML for one of the combo boxes.  Below that is the defintion of the ProjectResource class.  Just to recap, Names is an ObservableCollection<ProjectResource> and ProductManager is of type ProjectResource.

        EmptyText="Select Product Manager"
        Style="{StaticResource ContactComboStyle}"
        ItemsSource="{Binding Path=Names}"
        SelectedItem="{Binding Path=ProductManager, Mode=TwoWay}">

    public partial class ProjectResource
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string FullName { get; set; }
        public string Phone { get; set; }
        public string Email { get; set; }
        public string Role { get; set; }

  2. Valeri Hristov
    Valeri Hristov avatar
    2252 posts

    Posted 16 Dec 2010 Link to this post

    Hi Greg,

    You could try overriding Equals() - I think this will resolve the problem. Something like this:
    public partial class ProjectResource
        public override bool Equals(object obj)
            var other = obj as ProjectResource;
            return other != null && other.FirstName == this.FirstName && ...

    RadComboBox uses IList.IndexOf to find which of its items is the same as the value of the SelectedItem property. IndexOf internally uses Equals, which by default compares its parameters by reference and if you create a new instance of ProjectResource for the SelectedItem property that is not present in the ItemsSource, IndexOf will always return -1. When you override Equals to compare the properties of the two instances IndexOf will work properly.

    Best wishes,
    Valeri Hristov
    the Telerik team
    Browse the videos here>> to help you get started with RadControls for Silverlight
Back to Top