none
Button in DataGrid not binding to a Class RelayCommand , which is collection in Main ViewModel

    Question

  • Hi,

    I have ViewModel, Which has a ObservableCollection<Employee> EmpCol , now that ViewModel is bind with my View and and EmpCol is set as with ItemSource of custom control. That custom control generates stackpanels with grids , if there are 4 objects in EmpCol then there will be 4 stackpanels with Grids inside them. Now in those grids, I have a column of Buttons.
    Now my problem is I can't bind Command of that Button in datagrid to RelayCommand in Employee class.

    I am using MVVM light toolkit, and I found a way around which actually kind of give full path to binding command like this

        <Button x:Name="myButton"  Width="20" Height="15" HorizontalAlignment="Center"   VerticalAlignment="Center" Margin="5" >  
        <i:Interaction.Triggers>
         <i:EventTrigger EventName="Click">
        <cmd:EventToCommand Command="{Binding Source={StaticResource VMLocator}, Path=MyVM.Employee.MyButtonCommand}" PassEventArgsToCommand="False"/>  </i:EventTrigger>
        </i:Interaction.Triggers>
        </Button>




    Now this above approach works , but in my case I dont have Employee , but collection of Employee, so when I give binding path like this
                       Path=MyVM.EmpCol.MyButtonCommand

    It does't work. I have searched a lot , but could't find any solution.
    So any help will be much appreciated to solve my problem.

    Thanks,

    Maverick

    Friday, June 17, 2011 9:15 AM

Answers

  • Actually my datagrid is in datatemplate of another control. So using the StaticResource I am able to bind the RelayCommand , but my button in datagrid also need to send the context of Row on which this button is placed. And for this when I try to send the commandparamter as  " SelectedItem of ElementName = dgData" , it doesn't find that dataGrid.

    So how can I do this ? as I can't find the dgdata element so it comes SelectedItem comes as Null !!!

    You can pass the primary id of the record and in the viewModel you can use the primary id to find the other values of the record

    Thursday, June 23, 2011 7:18 AM

All replies

  • any one plz !

    Friday, June 17, 2011 3:27 PM
  • Try to Use The DataContext Instead of Path=MyVM.EmpCol.MyButtonCommand

    Command="{Binding ., Path=MyButtonCommand}"


    Friday, June 17, 2011 7:25 PM
  • I tried, it doesn't work.

    problem is binding of button inside datagrid , if I try to bind grid's events or other button events it works fine . Problem only happens when I try to bind button's event which is inside datagrid.

    Saturday, June 18, 2011 12:07 AM
  • Share your Control Code...

    Sunday, June 19, 2011 4:48 AM
  • Here is working example of binding command Inside datagrid

    http://asimsajjad.blogspot.com/2011/06/binding-command-inside-datagrid-using.html


    I think you should add the viewModel in the user control/page as resource and then in the binding of the datagrid or the command use the source property of the binding to bind the properties or collections.

    Sunday, June 19, 2011 6:59 AM
  • Hi,

    Because of the datagrid columns is collection property so that it can't derive parent DataContect

    Both should bind to StaticResource UserControl and Button

    <Button x:Name="myButton"  Width="20" Height="15" HorizontalAlignment="Center"   VerticalAlignment="Center" Margin="5" >   
       
    <i:Interaction.Triggers> 
         
    <i:EventTrigger EventName="Click"> 
       
    <cmd:EventToCommand Command="{Binding Source={StaticResource VMLocator}, Path=MyVM.Employee.MyButtonCommand}" PassEventArgsToCommand="False"/>  </i:EventTrigger> 
       
    </i:Interaction.Triggers> 
       
    </Button>

    Monday, June 20, 2011 6:40 AM
  • Actually my datagrid is in datatemplate of another control. So using the StaticResource I am able to bind the RelayCommand , but my button in datagrid also need to send the context of Row on which this button is placed. And for this when I try to send the commandparamter as  " SelectedItem of ElementName = dgData" , it doesn't find that dataGrid.

    So how can I do this ? as I can't find the dgdata element so it comes SelectedItem comes as Null !!!

    Wednesday, June 22, 2011 12:30 PM
  • Hi,

    If you need multiple parameter or a collection of Data that should retrive from ViewModel not from View. Because general class does not has function to do that except create custom Iteraction, behavior, trigger to forward to ViewModel.

    Thursday, June 23, 2011 6:13 AM
  • Actually my datagrid is in datatemplate of another control. So using the StaticResource I am able to bind the RelayCommand , but my button in datagrid also need to send the context of Row on which this button is placed. And for this when I try to send the commandparamter as  " SelectedItem of ElementName = dgData" , it doesn't find that dataGrid.

    So how can I do this ? as I can't find the dgdata element so it comes SelectedItem comes as Null !!!

    You can pass the primary id of the record and in the viewModel you can use the primary id to find the other values of the record

    Thursday, June 23, 2011 7:18 AM