This project is read-only.

DataGridTemplatedColumn with ComboBox DataBinding issue.

Apr 1, 2015 at 8:11 AM
Edited Apr 1, 2015 at 8:14 AM

I am using my toolkit for my Universal store app. I am facing a data binding issue with DataGridTemplatedColumn with ComboBox. Below is the sample code used for data binding.

<controls:DataGrid x:Name="DataGrid"
                               ItemsSource="{Binding Students,
                    <controls:DataGridTextColumn Width="*"
                                                 Binding="{Binding StudentID}"
                                                 HeaderStudent Id"
                                                 Style="{StaticResource BodyTextStyle}" />
controls:DataGridTemplatedColumn Width="*" Header="Student Name">
                                <ComboBox DisplayMemberPath="Name" 
                                          ItemsSource="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=StudentNames}" />

Combox is showing on the DataGrid but data not binding to the ComboBox. Please provide help on DataGridTemplatedColumn with ComboBox Data Binding issue.
Apr 1, 2015 at 8:32 AM
Edited Apr 1, 2015 at 8:33 AM
You cannot use TemplatedParent in a DataTemplate only in the ControlTemplate.

Apr 1, 2015 at 10:48 AM
I tried like below one but still DataContext not binding to the control which is placed inside DataGridTemplatedColumn.

                            <ComboBox DisplayMemberPath="Name" 
                                      ItemsSource="{Binding DataContext.StudentNames}" />

Apr 1, 2015 at 11:04 AM
This does not work because the current context inside the DataTemplate is an item from the collection and not the outer control or your outer view model...
Apr 1, 2015 at 11:05 AM
Have you tried to use ElementName?

Other possibility is to use a static resource as proxy element
Apr 1, 2015 at 11:49 AM
ruster, I tried with ElementName still no result. More over I removed comboBox in DataGridTemplatedColumn and placed button attached with viewmodel command. Button command not working. Here is the sample code piece.

In ViewModel declared DelegateCommand like below one.

public DelegateCommand<object> EditCommand { get; set; }

In ViewModel constructor assigned method to the EditCommand like below.

EditCommand = new DelegateCommand<object>(EditInformation);

private void EditInformation(object obj)
 // Do some work here.....

In Xaml

<controls:DataGridTemplatedColumn Width="*" Header="Edit">
                              <Button Width="auto"
                                        Command="{Binding EditCommand,
                                                          ElementName=View}" ----> Page
                                        Content="Edit" />
Problem is EditCommand not invoking.
Apr 1, 2015 at 12:05 PM
Is the view model object assigned to the DataContext property?

Then you need
Command="{Binding DataContext.EditCommand, ElementName=View}" 
If you assign the view model as static resource, you can easily bind to properties from everywhere in the view (also in a datatemplate). Have a look at this article:
Marked as answer by sudhakarkoganti on 4/7/2015 at 7:52 AM