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

Populate RadListBox in EditAppointmentTemplate (MVVM)

6 Answers 60 Views
ScheduleView
This is a migrated thread and some comments may be shown as answers.
Art Kedzierski
Top achievements
Rank 2
Art Kedzierski asked on 22 Aug 2013, 03:17 PM
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?

6 Answers, 1 is accepted

Sort by
0
Kalin
Telerik team
answered on 26 Aug 2013, 02:29 PM
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 >>
0
Art Kedzierski
Top achievements
Rank 2
answered on 26 Aug 2013, 07:04 PM
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. 
0
Accepted
Kalin
Telerik team
answered on 28 Aug 2013, 11:58 AM
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 >>
0
Art Kedzierski
Top achievements
Rank 2
answered on 28 Aug 2013, 12:50 PM
I have attached the current project to support ticket #731268.

Regards,
Art Kedzierski
0
Yana
Telerik team
answered on 29 Aug 2013, 12:10 PM
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 >>
0
Art Kedzierski
Top achievements
Rank 2
answered on 04 Sep 2013, 06:42 PM
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!
Tags
ScheduleView
Asked by
Art Kedzierski
Top achievements
Rank 2
Answers by
Kalin
Telerik team
Art Kedzierski
Top achievements
Rank 2
Yana
Telerik team
Share this question
or