Welcome to the Xceed Community | Help
Community Search  
More Search Options

Capturing Click event of Button in a DataCell using MVVM

Sort Posts: Previous Next
  •  03-23-2009, 3:42 PM Post no. 19536

    Capturing Click event of Button in a DataCell using MVVM

    Hello,

    I am using Model View ViewModel pattern. I have a button inside a DataCell of Xceed DataGrid. I want to use DelegateCommand to handle the click event of the button.

    Also, I dont want to write anything for the same in my Code Behind. I want everything to reside in my ViewModel.

    How can this be achieved. I thought of RoutedEvents / RoutedCommands but they need some entry in the Code Behind file.

    This I want to eliminate.

     Requesting you to please help on the same.

     

    Thanks and Regards

    SSA.

  •  03-23-2009, 5:07 PM Post no. 19541 in reply to 19536

    Re: Capturing Click event of Button in a DataCell using MVVM

    Everyone,

     Requesting you to please reply to this query. This would help using Xceed Datagrid in a more useful manner.

    Thanking you all in advance.

    Thanks and Regards

    SSA.

  •  03-25-2009, 10:41 AM Post no. 19626 in reply to 19541

    Re: Capturing Click event of Button in a DataCell using MVVM

    Hi Shailesh,

       The information you provided is not sufficient to help you without any doubts.

       However, we can work on the basics of the setup you describe, and try to suggest an approach.

       The "natural" approach to handle what you are refering to is indeed through the use of DelegateCommand or another similar class. The trick is that the button in the Cell need to have access to the DataContext for your View (typically the ViewModel), so he can bind his Command property to the DelegateCommand.

       Because the DataGrid for WPF is a complex control in itself, the DataContext from the Cell is NOT the DataContext from the DataGridControl. A quick way to compensate for this is to rebind the DataContext of the button to that of the DataGridControl.

      Note: This assumes that the ViewModel is the DataContext of the DataGridControl.

      <Button DataContext="{Binding RelativeSource={RelativeSource Self}, Path=(xcdg:DataGridControl.ParentDataGridControl).DataContext}" 
                   Command="{Binding MyCommandFromViewModel}" />
      
      Alternatly, you could also use a "FindAncestor" binding, but I prefer the first approach, since the FindAncestor binding resolves "later" than other types of binding.

      <Button DataContext="{Binding RelativeSource={RelativeSource AncestorType={x:Type xcdg:DataGridControl}}, Path=DataContext}"
                   Command="{Binding MyCommandFromViewModel}" />

     Note2: Code provided is only provided as example, I have not compiled code above.

    Marc Laroche
    Software Developer
    Xceed Software Inc.


    I don’t suffer from insanity, I enjoy every minute of it. - Unknown
  •  04-02-2009, 1:23 PM Post no. 19875 in reply to 19626

    Re: Capturing Click event of Button in a DataCell using MVVM

    Hello Marcus,

    Thanks for your reply and it definetly helps for an issue which could creep later on in my scenario. But the problem which I was having is that I have ViewModel which contains many properties out of which one property is a DelegateCommand say ViewCommand. Also, the datagrid is bound to one of the properties of the ViewModel say a Collection employeesCollection.

    Now , in that scenario if I want to bind the button inside the Cell of the DataGrid to the ViewCommand exposed by my ViewModel, how do I do it. I need the DataContext of the Window/UserControl at the DataGrid level .

     Would appreciate if you could provide some pointers on this.

    Thanks and Regards

    Shailesh S. Agaskar

  •  04-28-2010, 11:16 AM Post no. 26636 in reply to 19875

    Re: Capturing Click event of Button in a DataCell using MVVM

    I have the following:

    <xcdg:UnboundColumn FieldName="RemoveProposedOrderCommand" Title="Actions">

    <xcdg:UnboundColumn.CellContentTemplate>

    <DataTemplate>

    <Button

    DataContext="{Binding RelativeSource={RelativeSource Self}, Path=(xcdg:DataGridControl.ParentDataGridControl).DataContext}"

    Command="{Binding Path=RemoveProposedOrderCommand, Converter={StaticResource debugConverter}}"

    Template="{StaticResource RemoveButtonTemplate}" Cursor="Hand" Width="30"/>

    </DataTemplate>

    </xcdg:UnboundColumn.CellContentTemplate>

    </xcdg:UnboundColumn>

    and it throws a null reference exception on load.

  •  04-28-2010, 11:19 AM Post no. 26637 in reply to 19875

    Re: Capturing Click event of Button in a DataCell using MVVM

    I have the following:

    <xcdg:UnboundColumn FieldName="RemoveProposedOrderCommand" Title="Actions">

    <xcdg:UnboundColumn.CellContentTemplate>

    <DataTemplate>

    <Button

    DataContext="{Binding RelativeSource={RelativeSource Self}, Path=(xcdg:DataGridControl.ParentDataGridControl).DataContext}"

    Command="{Binding Path=RemoveProposedOrderCommand, Converter={StaticResource debugConverter}}"

    Template="{StaticResource RemoveButtonTemplate}" Cursor="Hand" Width="30"/>

    </DataTemplate>

    </xcdg:UnboundColumn.CellContentTemplate>

    </xcdg:UnboundColumn>

    and it throws a null reference exception on load.

View as RSS news feed in XML
Contact | Site Map | Reviews | Legal Terms of Use | Trademarks | Privacy Statement Copyright 2011 Xceed Software Inc.