Populate RadListBox in EditAppointmentTemplate (MVVM)

7 posts, 1 answers
  1. Art Kedzierski
    Art Kedzierski avatar
    107 posts
    Member since:
    Nov 2009

    Posted 22 Aug 2013 Link to this post

    I'm trying to populate a RadListBox in a custom EditAppointmentTemplate but I can't get the ItemSource to bind. I've seen similar threads that propose code-behind solutions, but I am using MVVM. 

    <telerik:RadListBox x:Name="_ApptEquipment_List"
              Grid.Row="1"
           Grid.Column="0"
                Margin="10,5,5,5"
           ItemsSource="{Binding AvailableEquipment}"
     SelectedValuePath="ID"
     DisplayMemberPath="Name"/>

    How would I bind to the view model from deep within the EditAppointmentTemplate without code-behind?
  2. Kalin
    Admin
    Kalin avatar
    1208 posts

    Posted 26 Aug 2013 Link to this post

    Hi Art,

    In order to set the DataContext of a ListBox inside the ControlTemplate you will have to declare the ViewModel as a static resource and then to bind the ListBox to it. Please see the code-snippet below:

    <UserControl.Resources>
        <local:ViewModel x:Name="ViewModelSource" />
        ...
        <telerik:RadListBox
                     DataContext=
    "{Binding Source={StaticResource ViewModelSource}}"
             ItemsSource="{Binding ListBoxItems}"/>
    </UserControl.Resources>

    In this case you can also set the DataContex to whole MainPage in the code-behind as follows:

    this.DataContext = this.Resources["ViewModelSource"] as ViewModel;

    Hope this helps.

    Regards,
    Kalin
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for SILVERLIGHT.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
  3. DevCraft banner
  4. Art Kedzierski
    Art Kedzierski avatar
    107 posts
    Member since:
    Nov 2009

    Posted 26 Aug 2013 Link to this post

    The app breaks on start-up whenever I attempt to reference the view-model from within the user control as you suggest:

    <UserControl.Resources>
        <vm:ReserveViewModel x:Key="ReserveViewModel" />
    </UserControl.Resources>

    <telerik:RadListBox Grid.Row="1"
                  Grid.Column="1"
                  Margin="5,5,10,5"
                  DataContext="{Binding Source={StaticResource ReserveViewModel}}"
                  ItemsSource="{Binding AvailableEquipment, Mode=OneWay}"
                  DisplayMemberPath="Name"
                  SelectedValuePath="ID"/>

    The app is based on the SofiaCarRentalDatabase example and the main view model bindings occur much higher in the project. 
  5. Answer
    Kalin
    Admin
    Kalin avatar
    1208 posts

    Posted 28 Aug 2013 Link to this post

    Hi Art,

    The ReserveViewModel and the EditAppoinmentTemplate should be defined in the same resource in order to work correctly, i.e. if you are setting the DataContext in the UserControl.Resources the ControlTemplate should be also there below the DataContext. If this isn't the case I'll ask you to provide the xaml code where the template and DataContext are defined in order to assist you further.

    I'm looking forward to your response.

    Regards,
    Kalin
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for SILVERLIGHT.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
  6. Art Kedzierski
    Art Kedzierski avatar
    107 posts
    Member since:
    Nov 2009

    Posted 28 Aug 2013 Link to this post

    I have attached the current project to support ticket #731268.

    Regards,
    Art Kedzierski
  7. Yana
    Admin
    Yana avatar
    4554 posts

    Posted 29 Aug 2013 Link to this post

    Hi Art,

    Thank you, we will check the ticket.

    Regards,
    Yana
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for SILVERLIGHT.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
  8. Art Kedzierski
    Art Kedzierski avatar
    107 posts
    Member since:
    Nov 2009

    Posted 04 Sep 2013 Link to this post

    Regarding the fix suggested in the ticket, I am now passing all the needed value lists into the occurrence. It's working so far, but I'm using a small test data set at the moment. We'll see how it handles an ObservableCollection of a few thousand users records (for a RadAutoCompleteBox people picker) once it gets into production.

    Thanks!
Back to Top
DevCraft banner