【问题标题】:I dont want to see dotted lines in Button edges我不想在按钮边缘看到虚线
【发布时间】:2025-12-22 08:55:12
【问题描述】:

1- 创建 vb.net / wpf 应用程序。

2- 创建三个 WPF 窗口,分别为 Window1Window2Window3

3- 将以下 xaml 代码复制并粘贴到 MainWindow

<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
    <StackPanel Width="180" Height="120">
        <Button x:Name="Button1" Height="30" Margin="5" Content="Button1"/>
        <Button x:Name="Button2" Height="30" Margin="5" Content="Button2"/>
        <Button x:Name="Button3" Height="30" Margin="5" Content="Button3"/>
    </StackPanel>
</Grid>
</Window>

4- 将以下 vb.net 代码复制并粘贴到 MainWindow 代码后面。

Class MainWindow

Private Sub Button1_Click(sender As Object, e As RoutedEventArgs) Handles Button1.Click
    Dim myWindow1 As New Window1()
    myWindow1.Show()
End Sub

Private Sub Button2_Click(sender As Object, e As RoutedEventArgs) Handles Button2.Click
    Dim myWindow2 As New Window2()
    myWindow2.Show()
End Sub

Private Sub Button3_Click(sender As Object, e As RoutedEventArgs) Handles Button3.Click
    Dim myWindow3 As New Window3()
    myWindow3.Show()
End Sub

Private Sub MainWindow_KeyDown(sender As System.Object, e As System.Windows.Input.KeyEventArgs) Handles MyBase.KeyDown
    If e.Key = Key.F1 Then
        Dim myWindow1 As New Window1()
        myWindow1.Show()
    End If

    If e.Key = Key.F2 Then
        Dim myWindow2 As New Window2()
        myWindow2.Show()
    End If

    If e.Key = Key.F3 Then
        Dim myWindow3 As New Window3()
        myWindow3.Show()
    End If
End Sub

End Class

5- 运行这个项目,然后点击Button2,然后关闭Window2,然后按F3,然后关闭Window3强>

我的问题:

我不想在按钮边缘看到虚线,如您在此处看到的 https://prnt.sc/lz8856

【问题讨论】:

  • 这就是你的“标签索引”在 button2 上的原因吗?将您的“TabIndex”位置更改为不同的控件。

标签: c# wpf vb.net xaml


【解决方案1】:

这条虚线是你的焦点。有不同的方法可以摆脱它。

  1. 在 XAML 中为您的按钮设置 IsTabStop="False",然后当您在您的按钮上 [Tab] 时将跳过它,但在鼠标单击后将保持用户焦点。因此,例如,鼠标点击后,您可以通过[Space bar]按下此按钮。

  2. 在 XAML 中为您的按钮设置 Focusable="False",然后当您在您的按钮上[Tab] 时将跳过它,并且将在鼠标单击后保持用户焦点。

  3. 在 XAML 中为您的按钮设置 FocusVisualStyle="{x:Null}",然后删除此行,您的 Tab 键顺序和焦点保持将保持不变。

【讨论】: