Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF DataGrid PreviewMouseDown not handling right click as expected

I have a DataGrid set up with a PreviewMouseDown event in a User Control. The idea is that when a user moves their mouse into a cell, it sets an Action to run based on the cell's contents, and the Action will run when the user clicks, regardless of which mouse button.
My event handler(C#):

private void LadderMouseClick(object sender, System.Windows.Input.MouseButtonEventArgs e)  {
        if (m_ActiveAction != null) {
            m_ActiveAction();
        }
        e.Handled = true;
    }

My DataGrid(XAML):

<DataGrid AutoGenerateColumns="False" CanUserAddRows="False" CanUserDeleteRows="False" CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserSortColumns="False"  EnableRowVirtualization="True"  ItemsSource="{Binding Source={StaticResource sourceCollection}}" Name="DataGrid" RowDetailsVisibilityMode="Collapsed" IsReadOnly="True" Height="600" MouseLeave="DataGrid_MouseLeave" MouseEnter="DataGrid_MouseEnter" PreviewMouseDown="LadderMouseClick" PreviewMouseDoubleClick="LadderMouseDoubleClick" VerticalScrollBarVisibility="Hidden" SelectionUnit="Cell" SelectionMode="Single">

This works, and runs the action properly when a user either right or left clicks over the DataGrid. The problem, however, is that when the user right clicks, the cell their mouse is in becomes selected(a black border appears around the cell); this doesn't happen when they left click.

Why are they behaving differently? Shouldn't they be handled the same way? Is there something really simple I'm missing?

Thanks in advance!

EDIT:
I've so far tried DataGrid.UnselectAll(), DataGrid.UnselectAllCells(), DataGrid.SelectedIndex = -1, and DataGrid.CurrentCell = default(DataGridCellInfo). None of them have worked for me.

EDIT #2:
If it helps, I'm using DataGridTemplateColumns with TextBlocks to display my data. Could this be causing this behaviour?

SOLUTION:
This was solved by Peter Hansen's suggestion which was to add a PreviewMouseRightButtonDown event to the DataGrid as well as the PreviewMouseDown event.

like image 950
Matt Densmore-Crew Avatar asked Nov 01 '12 14:11

Matt Densmore-Crew


2 Answers

I think what you are seeing is the border of the cell, which is being displayed when it has focus.

You can remove it by setting its thickness to 0 like this:

<DataGrid.CellStyle>
    <Style TargetType="DataGridCell">
        <Setter Property="BorderThickness" Value="0" />
    </Style>
</DataGrid.CellStyle>

Update:
Apparently when you set the PreviewMouseDown event to be handled, the DataGrid only stops selections of cells when you use the left mousebutton.

You have to handle PreviewMouseRightButtonUp as well to stop rightclicks from selecting cells.

I think that would solve your problem?

like image 63
Peter Hansen Avatar answered Nov 09 '22 11:11

Peter Hansen


You should be able to call the UnselectAllCells method that comes with DataGrids. It simply looks like this:

dataGrid.UnselectAllCells();

Call this after performing your click action and it should remove the cell selection.

EDIT: I think your dataGrid.UnselectAllCells() isn't invoking because you are still in the PreviewMouseDown event. Try to make a corresponding PreviewRightMouseButtonUp event and unselect your cells there. The time in the event that you invoke this method could be crucial to it's success.

like image 31
Jason Higgins Avatar answered Nov 09 '22 12:11

Jason Higgins