Databinding to a combo box

8 posts, 0 answers
  1. Ian
    Ian avatar
    15 posts
    Member since:
    Jun 2009

    Posted 29 Sep 2009 Link to this post

    I have a RadTabItem and inside it, several text boxes and two RadComboBoxes.

    I set the DataContext of the RadTabItem and using the databinding syntax for the text boxes, the values are loaded correctly.  "John" appears for example in the first name textbox.  

    But for one combo box, I have the following, also included in the tab item:

      <telerikInput:RadComboBox x:Name="CmbStatus" Grid.Row="1" Grid.Column="1" ItemsSource="{Binding IncidentStatuses}" />

    The IncidentStatuses property contains three items, but these are never bound to the combo box.  If I manually set the CmbStatus.ItemsSource to use IncidentStatuses, the items are populated.  I don't want to have to write more code to do what the data binding expression should do.  If ItemsSource is set to a valid property, the combo box should be able to handle the hook up.  

    My question is why the combox box can't pick up the IncidentStatuses from the property of the DataContext in the tab like the other system controls (text box in this case).  
  2. Hristo Borisov
    Admin
    Hristo Borisov avatar
    298 posts

    Posted 29 Sep 2009 Link to this post

    Hello Ian,

    Have you tried setting the DisplayMemberPath to "IncidentStatuses"? If the problem persists, would you send us a code snippet or a project in which we can reproduce the issue? Thank you for contacting us.

    Kind regards,
    Hristo Borisov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. Ian
    Ian avatar
    15 posts
    Member since:
    Jun 2009

    Posted 29 Sep 2009 Link to this post

    DisplayMemberPath is needed when binding a collection.  You need to know the property of the object to display and setting this to your collection will not help anything.



  4. Kaloyan
    Admin
    Kaloyan avatar
    920 posts

    Posted 02 Oct 2009 Link to this post

    Hello Ian,

    This is a very strange behavior. You can add a IValueConverter to the Binding of the RadComboBox and check weather a date is received to the ItemsSource or not. If not, try to bind the DataContext of the RadComboBox
    DataContext="{Binding}", so then the control will share the same DataContext objects.

    Let us know if this helps.

    Best wishes,
    Kaloyan
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  5. Ian
    Ian avatar
    15 posts
    Member since:
    Jun 2009

    Posted 02 Oct 2009 Link to this post

    Thanks Kaloyan for your reply.

    I'll try as you suggest.  BTW, I opened a ticket and sent along two projects.  One shows the RadComboBox binding as expected.  The second is my actual code, which uses MVP.

    From googling, it appears that a few other developers have experienced issues with Silverlight in general when it comes to databinding and MVP & MVVP.  I couldn't get the binding to work with a Microsoft ComboBox either, so I'm thinking at this point that my issue is not a Telerik-related problem.
  6. Ian
    Ian avatar
    15 posts
    Member since:
    Jun 2009

    Posted 02 Oct 2009 Link to this post

    Following Kaloyan's suggestions didn't work.  Thought I'd share that in case anyone else runs across the problem.  I've included more details here and I want to repeat that I think the issue is with Silverlight's data binding architecture.

    Setting a custom IValueConverter didn't work on the combo box, but it did work on the text boxes.  I could hit the break point and see that a test string I was returning was in fact being used by the control.  The combo box's converter was never hit.  It's like the data context is always null for any control which supports list items.  

    I have this (pseudo-code):

    <TabItem Name="IncidentDetails">
    <TextBox x:Name="Title" Text="{Binding Path=Title}" /> 
       <RadComboBox x:Name="CmbIncidentStatus" 
                                    DisplayMemberPath="Name" 
                                    SelectedValuePath="Value" 
                                    DataContext="{Binding}" 
                                    ItemsSource="{Binding IncidentStatuses}"
                                    SelectedIndex="0" />
    </TabItem>

    So, in my case, the Title property of the Incident object (which is set via code as the tab's DataContext) is correctly populated, but the list items for the combo box never are (unless I do this from code behind).  The problem here is that while I can do things in code and manually handle select change events to update my incident object, it causes problems with validation.  Validation in SL 3 seems to rely on binding; if certain controls aren't being bound but need validation, the developer is forced to find hacks to work even though the other controls like textbox are able to perform.  I'm left with binding that sort of works in some cases and not others; I end up writing a lot of code to handle the exception cases.  I don't think SL is quite ready for prime time since I keep finding these little not-so edge cases where stuff stops working.  
  7. Miroslav
    Admin
    Miroslav avatar
    922 posts

    Posted 05 Oct 2009 Link to this post

    Hello Ian,

    I noticed in the project that you sent that the IncidentStatuses property is marked as Internal or private.

    This could be the reason why the binding will not work, could you make the property public?

    Also, when a binding fails, you normally see a trace message in the Output window describing what has gone wrong. Do you see any traces there in your case?

    Sincerely yours,
    Miroslav
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  8. Ian
    Ian avatar
    15 posts
    Member since:
    Jun 2009

    Posted 05 Oct 2009 Link to this post

    Thanks Miroslav!

    That fixed my issue -- thought I'd tried making things public before, but guess I didn't.

    The output window tip was also useful as well!

    Ian
Back to Top