2
votes

I'm using .Net 4.0 and have a DataGrid in a view. I have implemeneted this http://www.codeproject.com/Articles/42227/Automatic-WPF-Toolkit-DataGrid-Filtering to provide filtering. The ItemsSource for the DataGrid is an observable collection of my custom objects. Each row has a Button that when clicked, passes the selected custom object back via the CommandParameter.

<DataGridTemplateColumn.CellTemplate>
  <DataTemplate>
    <Button Command="{Binding Path=DataContext.DeleteMyCustomObjectCommand,RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type DataGrid}}}" CommandParameter="{Binding}"  Width="20">
      <Image Source="/MyThing;component/Images/delete.png" Height="16" Width="16"/>
    </Button>
  </DataTemplate>
</DataGridTemplateColumn.CellTemplate>

I want to be able to save the filter criteria using this solution http://www.codeproject.com/Articles/42227/Automatic-WPF-Toolkit-DataGrid-Filtering?msg=3342202#xx3342202xx but one of the calls requires a reference to the data grid

QueryController queryController=   DataGridExtensions.GetDataGridFilterQueryController(myGrid1);

As i'm using MVVM, I don't have a reference to the DataGrid in my ViewModel. My command execution code (in the ViewModel) looks like this at present

public void DeleteMyCustomObject(object param)
    {
        MyCustomObject m = param as MyCustomObject;
.....Deletion commands go here

Is there a way I can use multibindings on the CommandParameter of my Delete button to pass back the custom object from the current row, and a reference to the actual DataGrid (or is there a better solution).

Many Thanks

Mick

1
could you show me your DataGrid and how you assign the CellTemplate to it ? i can't seem to find out how , as for your question , does the DataGrid.SelectedItem changed when you Click the Button ?... if so you can save the SelectedItem in your ViewModel and send the Grid as a Parameter for your Command ... then you'd have both elements to work with ..eran otzap

1 Answers

2
votes

(1) bound the DataGrid.SelectedItem to a Property in you ViewModel .

(2) send the Grid as the CommandParameter .

 <DataGrid Grid.Column="2" Name="DG1" ItemsSource="{Binding}" SelectedItem="{Binding SelectedItem}"  AutoGenerateColumns="False" >
     <DataGrid.Columns>
         <DataGridTemplateColumn>
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <Button Command="{Binding Path=DataContext.DeleteMyCustomObjectCommand,RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type DataGrid}}}" 
                            CommandParameter="{Binding RelativeSource=
                                              {RelativeSource FindAncestor,
                                              AncestorType={x:Type DataGrid}}}"  Width="20">
                         <Image Source="/MyThing;component/Images/delete.png" Height="16" Width="16"/>
                   </Button>
                </DataTemplate>
           </DataGridTemplateColumn.CellTemplate>
       <DataGridTemplateColumn>
    <DataGrid.Columns>
</DataGrid>