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>
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}}" />
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.
精彩评论