Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding Command inside DataGridTemplateColumn

I have View(XAML included) attached to my ViewModel with commands. I need to invoke command when Button on DataGrid's row clicked. I'm using behaviors for this (regular commanding have same issue). When I click button on DataGrid - my command does not get fired.

To illustrate issue - I placed ListBox on a bottom with EXACT same binding stuff - and yes, command works. So, it's something with DataGrid/DataGridTemplateColumn

<Grid x:Name="LayoutRoot" Background="White" DataContext="{Binding}">
            <Grid.RowDefinitions>
                <RowDefinition Height="30" />
                <RowDefinition Height="*" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <StackPanel Orientation="Horizontal">
                <Button Content="Cancel" >
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="Click">
                            <ei:CallMethodAction MethodName="Cancel" TargetObject="{Binding}"/>
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                </Button>
            </StackPanel>
            <sdk:DataGrid AutoGenerateColumns="False" IsReadOnly="True" ItemsSource="{Binding Data}" Grid.Row="1">
                <sdk:DataGrid.Columns>
                    <sdk:DataGridTemplateColumn CanUserReorder="True" CanUserResize="True" CanUserSort="True" Width="Auto">
                        <sdk:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Button Content="Select">
                                    <i:Interaction.Triggers>
                                        <i:EventTrigger EventName="Click">
                                            <i:InvokeCommandAction Command="{Binding ElementName=Control, Path=DataContext.ItemSelectedCommand}" CommandParameter="{Binding}" />
                                        </i:EventTrigger>
                                    </i:Interaction.Triggers>
                                </Button>  
                            </DataTemplate>
                        </sdk:DataGridTemplateColumn.CellTemplate>
                    </sdk:DataGridTemplateColumn>
                    <sdk:DataGridTextColumn Binding="{Binding DeviceId}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Header="Device" Width="Auto" FontWeight="Bold" />
                    <sdk:DataGridTextColumn Binding="{Binding SerialNumber}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Header="Serial Number" Width="Auto" />
                    <sdk:DataGridTextColumn Binding="{Binding LastActivityOn}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Header="Last Activity" Width="Auto" />
                    <sdk:DataGridTextColumn Binding="{Binding ClientVersion}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Header="Client Version" Width="Auto" />
                    <sdk:DataGridTextColumn Binding="{Binding OSVersion}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Header="OS Version" Width="Auto" />
                </sdk:DataGrid.Columns>

            </sdk:DataGrid>
            <ListBox Grid.Row="2" ItemsSource="{Binding Data}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{Binding DeviceId}"></TextBlock>
                            <Button Content="Select">
                                <i:Interaction.Triggers>
                                    <i:EventTrigger EventName="Click">
                                        <i:InvokeCommandAction Command="{Binding ElementName=Control, Path=DataContext.ItemSelectedCommand}" CommandParameter="{Binding}" />
                                    </i:EventTrigger>
                                </i:Interaction.Triggers>
                            </Button>
                        </StackPanel>                        
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </Grid>
like image 836
katit Avatar asked Sep 28 '11 20:09

katit


2 Answers

I am pretty sure inside a DataGrid, you still need the DataContextProxy to make bindings working. The ElementName binding doesn't work (yes, it is working for ListBox templates, but not DataGrid, this is because DataGridTemplateColumn is not in the visual tree), even in Silverlight 4.

UPDATE

In Silverlight 5, DataContextProxy is not needed in a DataGrid thanks to the support of Ancestor binding.

Example

<Button Command="{Binding DataContext.CancelCommand, RelativeSource={RelativeSource AncestorType=sdk:DataGrid}}" />
like image 94
Justin XL Avatar answered Nov 09 '22 17:11

Justin XL


I think the problem is this line

<i:InvokeCommandAction Command="{Binding ElementName=Control, Path=DataContext.ItemSelectedCommand}" CommandParameter="{Binding}" />

cause in your XAML is no element defined with name Control (aka. x:Name="Control").

You should change ElementName=Control to ElementName=LayoutRoot if the command is defined on the DataContext that is bound to LayoutRoot.

I don´t know why this works in the ListBox.

like image 20
Jehof Avatar answered Nov 09 '22 18:11

Jehof