Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
335 views
in Technique[技术] by (71.8m points)

c# - WPF:箭头导航DataGridTemplateColumn(WPF: arrow navigation DataGridTemplateColumn)

I made a custom column in a DataGrid, which includes a Grid, which consists of one row, in which the TextBlock, Separator and another TextBlock are laid.

(我在DataGrid中创建了一个自定义列,其中包括一个Grid,该网格由一行组成,在该行中放置了TextBlock,Separator和另一个TextBlock。)

I was able to achieve normal movement through the elements using the Tab key:

(我可以使用Tab键在元素之间实现正常移动:) 在此处输入图片说明

Also, it turned out to do this with the mouse:

(而且,事实证明是使用鼠标来做到这一点的:) 在此处输入图片说明

However, this does not work with the arrow keys, when you try to move to the right, the focus disappears (I suspect that the cell itself is highlighted, the selection of which I turned off):

(但是,这不适用于箭头键,当您尝试向右移动时,焦点消失了(我怀疑单元格本身已突出显示,而我关闭了对它的选择):) 在此处输入图片说明

How to correctly configure the movement of elements nested in the DataGridTemplateColumn(two TextBlocks) using the arrows on the keyboard?

(如何使用键盘上的箭头正确配置嵌套在DataGridTemplateColumn(两个TextBlocks)中的元素的移动?)

Thanks.

(谢谢。)

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*"/>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="0.5*"/>
                            </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 Physical1}" 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 Physical2}" 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>

PS: the code is greatly truncated, so that it can simply display the problem.

(PS:代码被极大地截断,因此它可以简单地显示问题。)

  ask by Range translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...