How can I send multiple command parameters to Button inside a datagrid ?

Answered How can I send multiple command parameters to Button inside a datagrid ?

  • Friday, October 12, 2012 6:01 AM
     
     

    I

    <sdk:DataGrid x:Name="dgList" Grid.Column="0" Grid.Row="1" HeadersVisibility="All" IsReadOnly="True" Width="324" AutoGenerateColumns="False">
                        <sdk:DataGrid.Columns>
                            <sdk:DataGridTextColumn Header="Department ID" Binding="{Binding DepartmentID}"></sdk:DataGridTextColumn>
                            <sdk:DataGridTextColumn Header="Department Name" Binding="{Binding DepartmentName}"></sdk:DataGridTextColumn>
                            <sdk:DataGridTemplateColumn Header="">
                                <sdk:DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <Button x:Name="btnEdit" Content="Edit" CommandParameter="{Binding DepartmentName}" Click="btnEdit_Click">
                                        </Button>
                                    </DataTemplate>
                                </sdk:DataGridTemplateColumn.CellTemplate>
                            </sdk:DataGridTemplateColumn>
                        </sdk:DataGrid.Columns>
                    </sdk:DataGrid>

    I nee Button CommandParameter to send multiple parameters in button click event. How can I acheive this ?

All Replies

  • Friday, October 12, 2012 8:09 AM
     
     Answered

    You'll have to bind to a single parameter which is a class that has multiple properties

    for instance:

    // the binding itself:
    CommandParameter="{Binding DepartmentInfo}"

    // which exists in your VM as:
    public DepartmentInfo DepartmentInfo { get // yadda yadda yadda

    // which is an instance of:
    public class DepartmentInfo
    {
        public string DepartmentName { get... // etc
        public Employee DepartmentLeader { get... //etc
        public string DepartmentId { get... // etc
        // and whatever other properties you would need

    The entire DepartmentInfo instance would be passed as a parameter, and you could get any property as needed.

  • Friday, October 12, 2012 9:02 AM
     
     

    You'll have to bind to a single parameter which is a class that has multiple properties

    for instance:

    // the binding itself:
    CommandParameter="{Binding DepartmentInfo}"

    // which exists in your VM as:
    public DepartmentInfo DepartmentInfo { get // yadda yadda yadda

    // which is an instance of:
    public class DepartmentInfo
    {
        public string DepartmentName { get... // etc
        public Employee DepartmentLeader { get... //etc
        public string DepartmentId { get... // etc
        // and whatever other properties you would need

    The entire DepartmentInfo instance would be passed as a parameter, and you could get any property as needed.

    Hi thanks for reply, I tried your code as it is but I am getting null while fetching the DepartmentInfo in the data context of button.

  • Friday, October 12, 2012 9:59 AM
     
     

    http://stackoverflow.com/questions/7589025/binding-command-inside-datagridtemplatecolumn

    http://stackoverflow.com/questions/3724335/command-parameter-is-null-in-a-command-of-customdatagrid

    http://stackoverflow.com/questions/6583135/command-binding-inside-a-datagridtemplatecolumn

    http://stackoverflow.com/questions/6384821/button-command-not-binding-to-viewmodel-in-datgrid

  • Friday, October 12, 2012 10:48 AM
     
     

    Does the DataContext you're using actually have a property called DepartmentInfo?  Did you initialize it?  I didn't give you a copy/paste solution, just a concept.

  • Monday, October 15, 2012 1:43 AM
     
     

    Does the DataContext you're using actually have a property called DepartmentInfo?  Did you initialize it?  I didn't give you a copy/paste solution, just a concept.

    Well, it didn't have any property. I just created it by myself with the underlying properties. 

  • Monday, October 15, 2012 2:07 AM
     
     Answered

    n00raina

    I thought, you can try this.

    <Button CommandParameter="{Binding Path=DataContext, RelativeSource={RelativeSource Self}}" Click="Button_Click"/>

     

    Why not cast it from the button's DataContext?