Matthew Finlay
Matthew Finlay

Reputation: 3464

Custom Click Behavior in a DataGrid

I have a datagrid displaying the contents of a custom data type.

<DataGrid Name="TestGrid" ItemsSource="{Binding Source={StaticResource Data}}" AutoGenerateColumns="False">
  <DataGrid.Columns>
    <DataGridTextColumn Header="Value 1" Binding="{Binding Value1, StringFormat={}{0:C}}"/>
    <DataGridTextColumn Header="Value 2" Binding="{Binding Value2, StringFormat={}{0:#.00}}"/>
  </DataGrid.Columns>

I want to tie different commands to the MouseLeftButtonDown and MouseRightButtonDown events for each DataGridTextColumn. I can use a DataGridTemplateColumn's CellEditingTemplate to do some customization of input, but haven't been able to figure out how to get the behavior I want.

Edit: What I'm aiming for is a left click increments the value and a right click decrements it, the size of the +/- would vary column to column.

Edit 2: It seems that you can change styles based on mouse events using triggers, but this doesn't seem to work for changing the data itself. I'm leaning towards abandoning the datagrid and going with a ListView

Upvotes: 1

Views: 3627

Answers (1)

David
David

Reputation: 6124

Here are my thoughts on the subject:

  1. You do not seem to need any editing functionality in your dataGrid from what I understand. In this case, you would indeed be better off with a ListView.

  2. If you want to continue with your dataGrid, the easiest way to do would indeed be to use TextBoxColumn.CellStyle and add the event listener there. If you do this, you'll have to make sure you update the ViewModel's value in your handler.

Try sompething like this:

<DataGrid Name="TestGrid" ItemsSource="{Binding Source={StaticResource Data}}" AutoGenerateColumns="False">
     <DataGrid.Columns>
          <DataGridTextColumn Header="Value 1" Binding="{Binding Value1, StringFormat={}{0:C}}">
              <DataGridTextColumn.CellStyle>
                   <Style TargetType="DataGridCell">
                        <EventSetter Event="MouseLeftButtonDown" Handler="MyMouseLeftButtonDownHandler" />
                        <EventSetter Event="MouseRightButtonDown" Handler="MyMouseRightButtonDownHandler" />
                    </Style>
               </DataGridTextColumn.CellStyle>
          </DataGridTextColumn>
          <DataGridTextColumn Header="Value 2" Binding="{Binding Value2, StringFormat={}{0:#.00}}">
              <DataGridTextColumn.CellStyle>
                   <Style TargetType="DataGridCell">
                        <EventSetter Event="MouseLeftButtonDown" Handler="MyMouseLeftButtonDownHandler" />
                        <EventSetter Event="MouseRightButtonDown" Handler="MyMouseRightButtonDownHandler" />
                   </Style>
              </DataGridTextColumn.CellStyle>
          </DataGridTextColumn>
     </DataGrid.Columns>
</DataGrid>

or make your own MyDataGridTextBoxColumn class that inherits the original one, and add the style to the column's cellStyle in your column's xaml definition. (that way you have only one common portion of code)

then in the handlers, you can easily deduce the cell where you clicked and increment or decrement the viewModel accordingly.

Upvotes: 6

Related Questions