禁用数据网格记录项目中的焦点wpf

时间:2018-05-03 16:33:17

标签: c# wpf datagrid focus

嗨我有一个数据网格当我专注于一行并使用键盘移动行时会出现所选单元格周围的虚线。你可以看到下面的图片

enter image description here

这是我的数据网格:

<DataGrid x:Name="dataGrid" AutoGenerateColumns="False" HorizontalAlignment="Left"
                                      Background="#eceff5" RowHeaderWidth="0" AlternatingRowBackground="#fbfcfe"
                                      GridLinesVisibility="Horizontal" CanUserReorderColumns="False"
                                      CanUserResizeColumns="False" CanUserSortColumns="True" SelectionMode="Single" SelectionUnit="FullRow" 
                                      CanUserResizeRows="False" IsReadOnly="True" ColumnHeaderStyle="{DynamicResource DataGridColumnHeaderStyle}" 
                                      HorizontalGridLinesBrush="#dde8ef" CellStyle="{DynamicResource DataGridCellStyle}"
                                      Grid.ColumnSpan="2" Margin="10,75,10,10">
                            <DataGrid.Columns>
                                <DataGridTextColumn Binding="{Binding Path=Id}" Visibility="Hidden" />
                                <DataGridTextColumn Binding="{Binding Path=Name}" Header="نام" MinWidth="120" Width="auto" />
                            </DataGrid.Columns>
                        </DataGrid>

这是我的风格:

<Style x:Key="DataGridColumnHeaderStyle" TargetType="{x:Type DataGridColumnHeader}">
                <Setter Property="VerticalContentAlignment" Value="Center"/>
                <Setter Property="Background" Value="#6f7e95"/>
                <Setter Property="Padding" Value="7,5,7,5"/>
                <Setter Property="Foreground" Value="#ffffff"/>
                <Setter Property="FontSize" Value="14"/>
                <Setter Property="FontWeight" Value="Bold"/>
                <Setter Property="BorderThickness" Value="0,0,1,0"/>
                <Setter Property="BorderBrush" Value="#7c8ca6"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
                            <Grid>
                                <Themes:DataGridHeaderBorder BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" IsClickable="{TemplateBinding CanUserSort}" IsPressed="{TemplateBinding IsPressed}" IsHovered="{TemplateBinding IsMouseOver}" Padding="{TemplateBinding Padding}" SortDirection="{TemplateBinding SortDirection}" SeparatorBrush="{TemplateBinding SeparatorBrush}" SeparatorVisibility="{TemplateBinding SeparatorVisibility}">
                                    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                                </Themes:DataGridHeaderBorder>
                                <Thumb x:Name="PART_LeftHeaderGripper" HorizontalAlignment="Left" Style="{StaticResource ColumnHeaderGripperStyle}"/>
                                <Thumb x:Name="PART_RightHeaderGripper" HorizontalAlignment="Right" Style="{StaticResource ColumnHeaderGripperStyle}"/>
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>


<Style x:Key="DataGridCellStyle" TargetType="{x:Type DataGridCell}">
                <Setter Property="Background" Value="Transparent"/>
                <Setter Property="BorderBrush" Value="Transparent"/>
                <Setter Property="BorderThickness" Value="0"/>
                <Setter Property="Padding" Value="5"/>
                <Setter Property="FontSize" Value="13"/>
                <Setter Property="Foreground" Value="#354052"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type DataGridCell}">
                            <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="True">
                                <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
                <Style.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="Background" Value="#70b8f3"/>
                        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
                        <Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                    </Trigger>
                    <Trigger Property="IsKeyboardFocusWithin" Value="True">
                        <Setter Property="BorderBrush" Value="{DynamicResource {x:Static DataGrid.FocusBorderBrushKey}}"/>
                    </Trigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsSelected" Value="true"/>
                            <Condition Property="Selector.IsSelectionActive" Value="false"/>
                        </MultiTrigger.Conditions>
                        <Setter Property="Background" Value="#70b8f3"/>
                        <Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightBrushKey}}"/>
                        <Setter Property="Foreground" Value="#ffffff"/>
                    </MultiTrigger>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                    </Trigger>
                </Style.Triggers>
            </Style>

所以我怎么能解决这个问题?如果您看到图像,则会看到另一列。如何删除此列?目前,以下代码已删除以下列,但数据网格不适合屏幕

2 个答案:

答案 0 :(得分:1)

添加到您的样式以删除FocusVisualStyle

<Setter Property="FocusVisualStyle" Value="{x:Null}" />

答案 1 :(得分:0)

对我来说,解决方案是设置单元格的样式:

<Style x:Key="SaturdayCellStyle" TargetType="DataGridCell">
  <Setter Property="Focusable" Value="False"/>
</Style>

然后在DataGrid中输入

<DataGridTextColumn CellStyle="{StaticResource SaturdayCellStyle}" />