views:

960

answers:

1

Hi,

I am using Silverlight 3 to develop an application. In my app, I have a layout Grid (named "LayoutGrid") in which I have a DataGrid (named "PART_datagrid") with DataGridTemplateColumns. The LayoutGrid is set a DataContext in which there is a Ladders list as a property. This Ladders list is set as the ItemsSource for the PART_datagrid.

<Grid x:Name="LayoutRoot">
   <DataGrid x:Name="PART_datagrid" ItemsSource="{Binding Ladders}">
      ...
      <DataGridTemplateColumn>
         <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
               <Button Name="DeleteLadder" Click.Command="{Binding ElementName=LayoutRoot, Path=DataContext.DeleteLadderCommand}" />

Now in one of the DataGridTemplateColumns I have a button which should invoke a Command thats present in the LayoutGrid's DataContext. So I tried Element-To-Element binding on my DataTemplate button as follows

<Button Name="DeleteLadder" Click.Command="{Binding ElementName=LayoutRoot, Path=DataContext.DeleteLadderCommand}" />

But this does not seem to work. What I want to achieve is to handle the event of deletion of a DataGrid row at the parent DataContext level using the command.

Can someone pls suggest how do I proceed on this?

Thanks in advance...

A: 

I have the same issue. In my example below the hyperlink and the button in the datatemplate are both wired up to the same command, however only the hyperlink works:

BTW this issue appears in Silverlight. I have seen some solutions in WPF (http://stackoverflow.com/questions/581715/binding-to-a-command-in-a-datagrid) but these won't work in Silverlight.

<Grid x:Name="LayoutRoot"><ScrollViewer x:Name="PageScrollViewer" Style="{StaticResource PageScrollViewerStyle}">
        <StackPanel x:Name="ContentStackPanel">
            <HyperlinkButton Content="Edit Activity" Margin="40,0,0,0" 
                commands:Click.Command="{Binding EditActivityCommand}" 
                commands:Click.CommandParameter="{Binding CurrentActivity}">
            </HyperlinkButton>
            </StackPanel>
            <data:DataGrid x:Name="ActivitiesDataGrid" ItemsSource="{Binding PagedActivities, Mode=TwoWay}" SelectedItem="{Binding Path=CurrentActivity, Mode=TwoWay}" Height="500">                    
                <data:DataGrid.Columns>
                    <data:DataGridTemplateColumn Header="Edit" HeaderStyle="{StaticResource EditColumnHeaderStyle}">
                        <data:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Button commands:Click.Command="{Binding EditActivityCommand}" commands:Click.CommandParameter="{Binding CurrentActivity}">
                                    <Image>
Martin MacPherson
See the directions outlined in FredHirschfield's comments in the following thread for a workaround: http://compositewpf.codeplex.com/Thread/View.aspx?ThreadId=64514.The binding in the datatemplate can't find the right element to bind to so you need to give it some help. In WPF the relativesource markup extension allows you to do this quite easily (http://stackoverflow.com/questions/581715/binding-to-a-command-in-a-datagrid). However in Silverlight the relativeresource m-e is not so feature rich and is unable to help. The solution outlined in the post involves using a binding helper class.
Martin MacPherson
Thanks Martin for your reply. However I had read this post and was not very comfortable with the approach mentioned i.e. creating a BindingHelper class and all. So I did something like: <Button Name="DeleteLadder" Click.Command="{Binding Path=DeleteLadderCommand}" /> i.e. placed the DeleteLadderCommand command in my Ladder View Model. This Ladder View Model then raises a separate event and propogates it to its parent View Model, so that the parent VM can do its stuff.
Sudeep