DataGridTemplatedColumn with ComboBox DataBinding issue.

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

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"
                               MaxHeight="400"
                               DefaultOrderIndex="1"
                               FontSize="20"
                               HeaderBackground="Silver"
                               ItemsSource="{Binding Students,
                                                     Mode=TwoWay}">
controls:DataGrid.Columns>
                    <controls:DataGridTextColumn Width="*"
                                                 Binding="{Binding StudentID}"
                                                 CanSort="True"
                                                 HeaderStudent Id"
                                                 Style="{StaticResource BodyTextStyle}" />
controls:DataGridTemplatedColumn Width="*" Header="Student Name">
                        <controls:DataGridTemplatedColumn.CellTemplate>
                            <DataTemplate>
                                <ComboBox DisplayMemberPath="Name" 
                                          ItemsSource="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=StudentNames}" />
                            </DataTemplate>
                        </controls:DataGridTemplatedColumn.CellTemplate>
                    </controls:DataGridTemplatedColumn>
</controls:DataGrid.Columns></controls:DataGrid>

Combox is showing on the DataGrid but data not binding to the ComboBox. Please provide help on DataGridTemplatedColumn with ComboBox Data Binding issue.
Coordinator
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.

See
http://stackoverflow.com/questions/18509611/how-bind-to-templatedparent-from-datatemplate-defined-in-a-style
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.

<controls:DataGridTemplatedColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox DisplayMemberPath="Name" 
                                      ItemsSource="{Binding DataContext.StudentNames}" />
                        </DataTemplate>
                    </controls:DataGridTemplatedColumn.CellTemplate>
                </controls:DataGridTemplatedColumn>

Coordinator
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...
Coordinator
Apr 1, 2015 at 11:05 AM
Have you tried to use ElementName?

http://stackoverflow.com/questions/15366609/how-to-access-parents-datacontext-in-window-8-store-apps

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">
                        <controls:DataGridTemplatedColumn.CellTemplate>
                            <DataTemplate>
                              <Button Width="auto"
                                        Height="auto"
                                        Command="{Binding EditCommand,
                                                          ElementName=View}" ----> Page
                                        Content="Edit" />
                            </DataTemplate>
                        </controls:DataGridTemplatedColumn.CellTemplate>
                    </controls:DataGridTemplatedColumn>
Problem is EditCommand not invoking.
Coordinator
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:
http://blog.rsuter.com/recommendations-best-practices-implementing-mvvm-xaml-net-applications/
Marked as answer by sudhakarkoganti on 4/7/2015 at 7:52 AM