I have WPF datagrid with multiple selections (the model has property IsSelected…) and it works fine, but when I start the program, I have to click on the table first, and after that, multiple selection works. When I first click on the table, it selects the item under the cursor (if I have pressed shift, it selects the item too, but it does not do multiple selections).
I supposed it can be because of datagrid is not focused, but when I do datagrid.Focus() on the loaded window, it doesn’t work either. What should I do?
WPF DataGrid code:
<DataGrid x:Name="datagrid" AutoGenerateColumns="False" Height="Auto" VerticalAlignment="Stretch" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="4" Margin="3"
CanUserAddRows="False" CanUserDeleteRows="False"
ItemsSource="{Binding Path=Items}" SelectedItem="{Binding Path=SelectedItem}" IsSynchronizedWithCurrentItem="True"
SelectedIndex="{Binding Path=SelectedIndex}" SelectionMode="Extended" SelectionUnit="FullRow" EnableColumnVirtualization="False" EnableRowVirtualization="False">
<DataGrid.Columns>
<DataGridTextColumn Header="{Binding CodebookUnitsUnitTable, Source={StaticResource Texts}}" Binding="{Binding Path=Unit}" Width="*" IsReadOnly="True"/>
<DataGridTextColumn Header="{Binding CodebookUnitsDescriptionTable, Source={StaticResource Texts}}" Binding="{Binding Path=Description}" Width="5*" IsReadOnly="True"/>
</DataGrid.Columns>
<DataGrid.CommandBindings>
<CommandBinding Command="SelectAll" Executed="CommandBinding_Executed"/>
</DataGrid.CommandBindings>
<DataGrid.ContextMenu>
<ContextMenu>
<MenuItem Header="{Binding CodebookContextMenuNewItem, Source={StaticResource Texts}}" Command="{Binding NewItemCommand}">
<MenuItem.Icon>
<myComponents:AutoGreyableImage Height="20" Margin="0,-1,0,0" Width="20" Source="../Icons/Small/INovySM.gif" Stretch="None" RenderOptions.BitmapScalingMode="NearestNeighbor" RenderOptions.EdgeMode="Aliased"/>
</MenuItem.Icon>
</MenuItem>
</ContextMenu>
</DataGrid.ContextMenu>
<DataGrid.InputBindings>
<KeyBinding Command="{Binding DeleteCommand}" Key="Delete"/>
</DataGrid.InputBindings>
</DataGrid>
The solution is set focused on the first row, not only on DataGrid.