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

Binding issue

1 Answer 50 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Greg Cockerham
Top achievements
Rank 1
Greg Cockerham asked on 14 Dec 2010, 11:22 PM
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.

<telerik:RadComboBox
    Grid.Row="1"
    Grid.Column="1"
    EmptyText="Select Product Manager"
    Style="{StaticResource ContactComboStyle}"
    ItemsSource="{Binding Path=Names}"
    DisplayMemberPath="FullName"
    SelectedItem="{Binding Path=ProductManager, Mode=TwoWay}">
     <telerik:RadComboBox.ItemsPanel>
          <ItemsPanelTemplate>
               <VirtualizingStackPanel/>
          </ItemsPanelTemplate>
     </telerik:RadComboBox.ItemsPanel>
</telerik:RadComboBox>

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


Thanks,
Greg

1 Answer, 1 is accepted

Sort by
0
Valeri Hristov
Telerik team
answered on 16 Dec 2010, 10:42 AM
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
Tags
ComboBox
Asked by
Greg Cockerham
Top achievements
Rank 1
Answers by
Valeri Hristov
Telerik team
Share this question
or