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.
How would I bind to the view model from deep within the EditAppointmentTemplate without code-behind?
<
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
0
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:
In this case you can also set the DataContex to whole MainPage in the code-behind as follows:
Hope this helps.
Regards,
Kalin
Telerik
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 >>
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:
The app is based on the SofiaCarRentalDatabase example and the main view model bindings occur much higher in the project.
<
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
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
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 >>
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
Regards,
Art Kedzierski
0
Hi Art,
Thank you, we will check the ticket.
Regards,
Yana
Telerik
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 >>
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!
Thanks!