Unable to multiselect (SelectionMode="Extended") row in a WPF datagrid

38 Views Asked by At

All of my datagrids have this problem, so it is obviously something I have done that is preventing multiselect, but I cannot figure it out. The only event that has code is Mouse_DoubleClick. I can only select one row at a time. Something is preventing multiselect, I don't think I have had a need for it before and all my grids have basically the same definition and they all seem to behave the same way.

     <DataGrid x:Name="FacilityGrid" Margin="0" Sorting="FacilityGrid_Sorting"
                   ItemsSource="{Binding Facilities}" SelectedItem="{Binding SelectedFacility}"
                   Height="Auto" Width="Auto" SelectionMode="Extended"  AutoGenerateColumns="False" CanUserAddRows="False" CanUserResizeColumns="True"
                   SelectionUnit="FullRow"  Background="Transparent" CanUserResizeRows="False" CanUserReorderColumns="False" CanUserSortColumns="False"
                   ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto" GridLinesVisibility="All"
                   VerticalAlignment="Stretch" AlternationCount="2" EnableColumnVirtualization="False" EnableRowVirtualization="True"
                   AlternatingRowBackground="WhiteSmoke" SelectionChanged="FacilityGrid_SelectionChanged">

         <DataGrid.ColumnHeaderStyle>
             <Style TargetType="{x:Type DataGridColumnHeader}">
                 <Setter Property="FontWeight" Value="SemiBold"/>
                 <Setter Property="HorizontalContentAlignment" Value="Center"/>
             </Style>
         </DataGrid.ColumnHeaderStyle>

         <DataGrid.CellStyle>
             <Style TargetType="DataGridCell" >
                 <Style.Triggers>
                     <Trigger Property="IsSelected" Value="True">
                         <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
                         <Setter Property="Background" Value="Transparent"/>
                         <Setter Property="BorderBrush" Value="Black"/>
                         <Setter Property="BorderThickness" Value="0,2,0,2"/>
                     </Trigger>
                 </Style.Triggers>
                 <Setter Property="FontWeight" Value="SemiBold"/>
                 <EventSetter Event="MouseDoubleClick" Handler="DataGridCell_MouseDoubleClick"/>
             </Style>
         </DataGrid.CellStyle>

         <DataGrid.RowStyle>
             <Style TargetType="DataGridRow">
                 <Setter Property="ContextMenu" Value="{StaticResource RowMenu}"/>
                 <Setter Property="Template">
                     <Setter.Value>
                         <ControlTemplate TargetType="DataGridRow">
                             <Border BorderThickness="{TemplateBinding Border.BorderThickness}" BorderBrush="{TemplateBinding Border.BorderBrush}" 
                                     Background="{TemplateBinding Panel.Background}" Name="DGR_Border" SnapsToDevicePixels="True">
                                 <SelectiveScrollingGrid>
                                     <DataGridCellsPresenter ItemsPanel="{TemplateBinding ItemsControl.ItemsPanel}" 
                                                             SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}"/>
                                 </SelectiveScrollingGrid>
                             </Border>
                         </ControlTemplate>
                     </Setter.Value>
                 </Setter>
                 <Setter Property="Height" Value="25"/>
                 <Setter Property="FontSize" Value="12"/>
                 <Setter Property="Visibility" Value="{Binding IsVisible, Converter={StaticResource BooleanToVisibilityConverter}}"/>
                 <Style.Triggers>
                     <Trigger Property="IsSelected" Value="True">
                         <Setter Property="Background" Value="Azure"/>
                     </Trigger>
                 </Style.Triggers>
             </Style>
         </DataGrid.RowStyle>

         <!--hide the default button at the beginning of each row-->
         <DataGrid.RowHeaderStyle>
             <Style TargetType="{x:Type DataGridRowHeader}">
                 <Setter Property="Width" Value="0"/>
             </Style>
         </DataGrid.RowHeaderStyle>

         <DataGrid.Columns>
 </DataGrid.Columns>
</DataGrid>
        private ObservableCollection<Facility> _Facilities = new ObservableCollection<Facility>();
        public ObservableCollection<Facility> Facilities
        {
            get
            { return _Facilities; }
            set
            {
                if (value != _Facilities)
                {
                    _Facilities = value;
                    NotifyPropertyChanged();
                }
            }
        }

        private Facility _SelectedFacility;
        public Facility SelectedFacility
        {
            get
            { return _SelectedFacility; }
            set
            {
                if (value != _SelectedFacility)
                {
                    _SelectedFacility = value;
                    NotifyPropertyChanged();
                }
            }
        }

        private void FacilityGrid_Sorting(object sender, DataGridSortingEventArgs e)
        {
            Mouse.OverrideCursor = Cursors.Wait;
            UIDispatcher.BeginInvoke((System.Action)(() => { Mouse.OverrideCursor = null; }),
                                     DispatcherPriority.ContextIdle);
        }

        private void DataGridCell_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            try
            {
                //this means it's on the main form
                var tabControl = Extensions.FindParent<TabControl>((DataGridCell)sender);
                if (tabControl != null)
                {
                    var facility = ((DataGridCell)sender)?.DataContext as Facility;
                    if (facility != null)
                    {
                        facility.RaiseOpenEvent();
                    }
                }
                else
                {
                    var facility = ((DataGridCell)sender)?.DataContext as Facility;
                    if (facility != null)
                    {
                        facility.RaiseSelectEvent();
                    }
                }
            }
            catch { }
        }

        private void FacilityGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var dg = sender as DataGrid;
            if (dg != null)
            {
                MainViewModel.GetMainViewModel().SelectedFacilityRow = (dg.SelectedIndex + 1).ToString("#,#");
            }
        }

0

There are 0 best solutions below