【发布时间】:2019-10-09 19:22:34
【问题描述】:
我在画布中有两个按钮(Focusable=true) 我使用 tab 键选择一个按钮,然后按 enter 删除该按钮 我希望(键盘)焦点回到画布,但它会转到窗口
演示: MainWindow.xaml
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.Resources>
<Style TargetType="Button">
<Setter Property="Background" Value="Red" />
<Style.Triggers>
<Trigger Property="IsKeyboardFocused" Value="True">
<Setter Property="Background" Value="Green" />
</Trigger>
</Style.Triggers>
</Style>
<Style TargetType="Canvas">
<Setter Property="Background" Value="Red" />
<Style.Triggers>
<Trigger Property="IsKeyboardFocused" Value="True">
<Setter Property="Background" Value="Green" />
</Trigger>
</Style.Triggers>
</Style>
</Grid.Resources>
<Canvas Name="Canvas" Focusable="True">
<Button Click="RemoveSelf">Button 1</Button>
<Button Canvas.Left="100" Click="RemoveSelf">Button 2</Button>
</Canvas>
</Grid>
</Window>
MainWindow.xaml.cs:
using System.Windows;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
}
private void RemoveSelf(object sender, RoutedEventArgs e)
{
Canvas.Children.Remove((UIElement)sender);
}
}
}
当应用程序启动时,一切都是红色的。 只要你按 TAB,画布就会变成绿色 再次按 TAB,Button 1 变为绿色,canvas 变为红色 按 ENTER,按钮 1 消失,但画布没有变成绿色 我想让画布变成绿色
【问题讨论】: