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

[Solved] GridViewComboBoxColumn - ItemsSourceBinding

3 Answers 368 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
JS
Top achievements
Rank 1
JS asked on 29 Jun 2010, 01:29 PM
Hi

I am trying to bind a GridViewComboBoxColumn to a property in my ViewModel.

For example:
A view "HomeView" with DataContext "HomeViewModel"
"HomeViewModel" has an observable collection "Tasks". Each task has a property "TypeId".
"HomeViewModel" has an observable collection "Types".

The GridView has ItemsSource="{Binding Path=Tasks}"
The GridView has a GridViewComboBoxColumn that should list all "Types".

If I use ItemsSource="{Binding Path=Types}" for the column, the column will be blank until focused.
When I click on the column, the values appear. When the column looses focus, the entire column is blank.

If I use ItemsSourceBinding="{Binding Path=Types}" for the column, the binding will search for a property "Types" in the "Task"

I have tried binding to "ElementName", and "Path=DataContext.Types", where ElementName refers to the View, but this did not work.

I have found these solutions to be working, but both might be a problem if I am using d:DataContext for designtime support:
ItemsSourceBinding="{Binding Path=FindShared[HomeViewModel].Types, Source={StaticResource VMLocator}}"
(http://johnpapa.net/silverlight/simple-viewmodel-locator-for-mvvm-the-patients-have-left-the-asylum/)
Each call to "FindShared" will result in 6 bindings, and will be a performance problem.

This seems to be a better solution:
ItemsSourceBinding="{Binding Path=DataSource.Types, Source={StaticResource DataContextProxy}}"
http://weblogs.asp.net/dwahlin/archive/2009/08/20/creating-a-silverlight-datacontext-proxy-to-simplify-data-binding-in-nested-controls.aspx


Do you have other better solutions for binding a ViewModel property to the ItemsSource in the GridViewComboBoxColumn?

regards
JS

3 Answers, 1 is accepted

Sort by
0
Pavel Pavlov
Telerik team
answered on 02 Jul 2010, 02:18 PM
Hi JS,

The root of the problem is that ElementName binding is not currently supported inside cells( due to cell virtualization) . Additionally the cells have the business object stored in the ItemsSource collection  as DataContext rather than the whole view model.

So there are two possible workarounds :

1. Set the ItemsSource  for the ComboBox column form codebehind , rather than binding in XAML .
OR
2. Expose the ItemsSource as static resource in XAML and use the  {Binding ..., Source= {StaticResource ...}}  syntax.

Sincerely yours,
Pavel Pavlov
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Peter
Top achievements
Rank 1
answered on 20 Sep 2010, 05:50 PM
Can you expand on #2 "Expose the ItemsSource as static resource in XAML and use the  {Binding ..., Source= {StaticResource ...}}  syntax."

Thanks,

Peter Pepperell
0
Yavor Georgiev
Telerik team
answered on 21 Sep 2010, 10:23 AM
Hello Peter,

 What you need to do is to expose your ViewModel as a StaticResource like so:
<UserControl.Resources>
  <local:HomeViewModel x:Key="MyViewModel" />
</UserControl.Resources>

 And then:
<telerik:GridViewComboBoxColumn DataMemberBinding="{Binding TypeId}" ItemsSourceBinding={Binding Types, Source={StaticResource MyViewModel}}" />

 If exposing your ViewModel as a StaticResource is unfeasible, you should instead use a DataContextProxy, as you cannot bind to a property of another element's DataContext in Silverlight.

Kind regards,
Yavor Georgiev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
GridView
Asked by
JS
Top achievements
Rank 1
Answers by
Pavel Pavlov
Telerik team
Peter
Top achievements
Rank 1
Yavor Georgiev
Telerik team
Share this question
or