tags:

views:

223

answers:

2

is there a way to mimic the behavior of ctrl+click which keeps previously selected rows selected and just adds more selected items?

by default, when clicking on each row, all previously selected rows get de-selected.

one way to achieve this, would be to override SelectionChanged event, and re-select removed rows.

void TestGrid_SelectionChanged(object sender, SelectionChangedEventArgs e) {
    foreach (var i in e.RemovedItems)
        TestGrid.SelectedItems.Add(i);

}

This is not ideal however, because in some situations i would want to de-select rows (such as when clicking a toggle button in one of the columns).

A: 

Set the SelectionMode to DataGridSelectionMode.Extended

Craig Suchanec
nope.. tried that.. only allows multiple selections while holding down ctrl
Sonic Soul
I misunderstood you question then. You want it to keep selecting items while they click to different items but not have them hold control?
Craig Suchanec
yes, i want to mimic the ctrl holding behavior, w/out having to hold ctrl.
Sonic Soul
I don't see an easy way for you to do it other than the method you suggested, plus adding logic for the toggle button which seems messy over all. The problem is that the DataGrid has a method ShouldMinimallyModifySelection that checks for the Ctrl key being pressed and that's how it makes the determination along with the selection mode. The only other idea I'd have is to trick it into thinking Ctrl is pressed when a new selection is being made.
Craig Suchanec
A: 

here's a solution that worked for me.

i removed all properties that set details visiblity (to keep everything at default)

than added the following style

<Style x:Key="VisibilityStyle" TargetType="{x:Type DataGridRow}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=Visible}" Value="False">
            <Setter Property="DetailsVisibility" Value="Collapsed" />
        </DataTrigger>
        <DataTrigger Binding="{Binding Path=Visible}" Value="True">
            <Setter Property="DetailsVisibility" Value="Visible" />
        </DataTrigger>
    </Style.Triggers>
</Style>

assigned this resource to RowStyle

in my underlying data object, i added Visible property, and Implemented INotifyPropertyChanged interface.

now whenever i want to show/hide details, i simply manipulate the Visible property on my underlying object. this can happen from column button handler, to anywhere else in my code. works great

Sonic Soul