none
Moved focus custom cell in DataGrid RRS feed

  • Question

  • Hello! I create DataGrid with custom column. My code:

    <DataGrid Grid.Row="1"  AutoGenerateColumns="False" ItemsSource="{Binding Report}"
                      CanUserResizeRows="False" SelectionMode="Single" IsReadOnly="True" HorizontalScrollBarVisibility="Disabled" HeadersVisibility="Column"
                      CanUserSortColumns="False">
                <DataGrid.Columns>
                    <DataGridTextColumn ElementStyle="{StaticResource WrapText}" IsReadOnly="true" Header="Number" Binding="{Binding Path=Number, Mode=TwoWay}" Width="*"/>
                    <DataGridTemplateColumn Width="*">
                        <DataGridTemplateColumn.CellStyle>
                            <Style TargetType="DataGridCell">
                                <Setter Property="BorderThickness" Value="0"/>
                                <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
                                <Setter Property="KeyboardNavigation.IsTabStop" Value="False"/>
                                <Setter Property="Margin" Value="0,0,-1,0"></Setter>
                            </Style>
                        </DataGridTemplateColumn.CellStyle>
                        <DataGridTemplateColumn.HeaderStyle>
                            <Style TargetType="{x:Type DataGridColumnHeader}">
                                <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                            </Style>
                        </DataGridTemplateColumn.HeaderStyle>
                        <DataGridTemplateColumn.HeaderTemplate>
                            <DataTemplate>
                                <Grid>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="*"/>
                                        <RowDefinition Height="Auto"/>
                                        <RowDefinition Height="*"/>
                                    </Grid.RowDefinitions>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="0.5*" x:Name="Column6"/>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition Width="0.5*" x:Name="Column7"/>
                                    </Grid.ColumnDefinitions>
                                    <TextBlock Background="Transparent" TextWrapping="Wrap" Grid.ColumnSpan="3" Text="Physics All" TextAlignment="Center" Margin="5,5,5,5"/>
                                    <Separator Background="{DynamicResource GridViewColumnHeaderBorderBackground}" Grid.ColumnSpan="3" Grid.Row="1" Margin="-4,0,-4,0"/>
                                    <TextBlock Background="Transparent" TextWrapping="Wrap" Grid.Row="2" Grid.Column="0"  Text="Physics 1" TextAlignment="Center" Margin="5,5,5,5"/>
                                    <StackPanel Orientation="Horizontal" Grid.Row="2" Grid.Column="1">
                                        <Separator Style="{StaticResource {x:Static ToolBar.SeparatorStyleKey}}" Background="{DynamicResource GridViewColumnHeaderBorderBackground}" Margin="0, -3,0,-3"/>
                                    </StackPanel>
                                    <TextBlock Background="Transparent" TextWrapping="Wrap" Grid.Row="2" Grid.Column="2" Text="Physics 2" TextAlignment="Center" Margin="5,5,5,5" />
                                </Grid>
                            </DataTemplate>
                        </DataGridTemplateColumn.HeaderTemplate>
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="*"/>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition Width="*"/>
                                    </Grid.ColumnDefinitions>
                                    <TextBox Grid.Column="0" Background="Transparent" Text="{Binding PhysicalDeterioration}" TextWrapping="Wrap" TextAlignment="Left"
                                             IsReadOnly="True" BorderThickness="1, 1.2, 1, 1.2" Cursor="Arrow" SelectionBrush="Transparent" BorderBrush="Transparent">
                                        <TextBox.ContextMenu>
                                            <ContextMenu Visibility="Collapsed">
                                            </ContextMenu>
                                        </TextBox.ContextMenu>
                                        <TextBox.Style>
                                            <Style TargetType="TextBox" BasedOn="{StaticResource textBoxNoBorderIsMouseOver}"/>
                                        </TextBox.Style>
                                    </TextBox>
                                    <StackPanel Orientation="Horizontal" Grid.Column="1">
                                        <Separator Background="Black" Margin="0, -1,0,-1" Visibility="{Binding Path=SeparatorIsVisible, Converter={StaticResource BoolToVisConverter} }">
                                            <Separator.Style>
                                                <Style TargetType="Separator" BasedOn="{StaticResource {x:Static ToolBar.SeparatorStyleKey}}"/>
                                            </Separator.Style>
                                        </Separator>
                                    </StackPanel>
                                    <TextBox Grid.Column="2" Background="Transparent" Text="{Binding PhysicalDeteriorationSred}" TextWrapping="Wrap" TextAlignment="Left"
                                             IsReadOnly="True" BorderThickness="1, 1.2, 1, 1.2" Cursor="Arrow" SelectionBrush="Transparent" BorderBrush="Transparent">
                                        <TextBox.ContextMenu>
                                            <ContextMenu Visibility="Collapsed">
                                            </ContextMenu>
                                        </TextBox.ContextMenu>
                                        <TextBox.Style>
                                            <Style TargetType="TextBox" BasedOn="{StaticResource textBoxNoBorderIsMouseOver}"/>
                                        </TextBox.Style>
                                    </TextBox>
                                </Grid>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                </DataGrid.Columns>
            </DataGrid>

    It looks like this:

    I want in my custom column, when the cell is selected and the mouse is moved, the focus of the cell will change as in the other cells. Here is a demo:

    Here you can see, when I move the mouse with the LMK pressed, the cells are highlighted with a black frame. But in the custom column there is no such effect. How to implement this? Thanks.

    Tuesday, December 10, 2019 11:02 AM

All replies

  • Hi,

    Do you want the DataGrid to move the mouse to get focus?You can try my code:

       private void DataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
            {
                e.Row.MouseEnter += Row_MouseEnter;
            }
            void Row_MouseEnter(object sender, MouseEventArgs e)
            {
                DataGridRow row = (DataGridRow)sender;
                Dispatcher.Invoke(new Action(delegate ()
                {
                    Datagrid1.SelectedIndex = row.GetIndex();
                    Datagrid1.Focus();
                }
                   ), System.Windows.Threading.DispatcherPriority.Background);
            }

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, December 11, 2019 5:43 AM
    Moderator
  • Thanks for your answer, but that's not it. I want to transfer focus to a cell, and at the clamped LMB key with movement of a mouse on the table. This is the default behavior of the DataGrid.
    Wednesday, December 11, 2019 3:49 PM
  • Hi,

    Do you want to select a single cell?You can try to use SelectionUnit Property of Datagrid.

    SelectionUnit="Cell"

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, December 12, 2019 9:02 AM
    Moderator
  • You don't quite understand what I want to achieve. Make a test application, place a DataGrid on it (and 2-3 columns), and fill it with any data. Now, launch the app, and click on any cell And(!!!) do not release the button. A black border will appear around the cell, and it will also come into focus. Now move the mouse (still with the mouse button pressed) to any other cell. Focus from the current cell will be removed and moved to the one over which your mouse cursor will now be located. This is exactly the behavior I want to achieve but with DataGridTemplateColumn.
    Thursday, December 12, 2019 9:57 AM
  • Hi,

    Your request is that after pressing the left mouse button, the focus changes with the mouse?

         private void DataGrid1_LoadingRow(object sender, DataGridRowEventArgs e)
    
            {
                
                e.Row.MouseEnter += Row_MouseEnter;
            }
            void Row_MouseEnter(object sender, MouseEventArgs e)
            {
                if (e.LeftButton == MouseButtonState.Pressed)
                {
                    DataGridRow row = (DataGridRow)sender;
                    Dispatcher.Invoke(new Action(delegate ()
                    {
                        DataGrid1.SelectedIndex = row.GetIndex();
                        DataGrid1.Focus();
                    }
                       ), System.Windows.Threading.DispatcherPriority.Background);
                }
                   
            }

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, December 16, 2019 7:47 AM
    Moderator
  • Sorry, but not again. After clicking the mouse button on any cell and moving the mouse on the Custom Cell(DataGridemplateColumn), this Custom Cell does not receive focus (the black border does not appear). And I want Custom Cell to get this trick :)

    Look at the detailed gif:

    I click on a standard cell, it gets into focus. I move the mouse to the cell below, and the focus moves with the mouse. But, when I move the focus to Custom Cell, with a value of 5/33 38/4, etc., the focus disappears (!!!). I want to get the behavior so that when you move the mouse and hold down the mouse button, the focus falls on these Custom Cells(with a value of 5/33 38/4, etc.)



    • Edited by range36 Monday, December 16, 2019 10:47 PM
    Monday, December 16, 2019 10:37 PM
  • Hi,

    I get what you meant now,I think a control in your Grid has got focus.

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, December 18, 2019 7:35 AM
    Moderator
  • Yes, but the visual control does not receive focus, and I unsuccessfully seek a solution to this problem ..
    Wednesday, December 18, 2019 9:29 AM