将焦点保持在WPF中的自定义(用户)控件上

时间:2011-04-14 10:54:35

标签: .net wpf wpf-controls mouse

说,我需要使用键盘在表单中移动自定义(用户)控件。

行为很简单:用户点击控件,然后可以使用键盘移动它(向上/ d / l / r)。

在此示例而不是UserControl中,我将使用Canvas。

问题在于,当我“聚焦”我的控件时,或者在文本框的“步骤”发生后我松开它,或者我根本没有收到它......

XAML:

<Window x:Class="WpfApplication14.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"
        PreviewKeyDown="Window_PreviewKeyDown">
    <Grid>
        <TextBox Name="textBox1" 
                 Margin="91,66,292,223" Width="120" 
                 Text="Good morning, miss Hilton!"/>
        <Canvas Name="canvas1" Focusable="True"
                Margin="124,112,179,99" Width="200"
                Background="AliceBlue"
                MouseDown="canvas1_MouseDown"
                PreviewKeyDown="canvas1_PreviewKeyDown"  />
    </Grid>
</Window>

CS:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void canvas1_MouseDown(object sender, MouseButtonEventArgs e)
    {
        Console.WriteLine("canvas_MouseDown 0, canvas1.IsFocused? {0}",
            canvas1.IsFocused);

        canvas1.Focus();

        Console.WriteLine("canvas_MouseDown 1, canvas1.IsFocused? {0}",
            canvas1.IsFocused);
    }

    private void canvas1_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        Console.WriteLine("canvas_PreviewKeyDown, canvas1.IsFocused? {0}",
            canvas1.IsFocused);

        if (canvas1.IsFocused)
            MoveCanvas(e.Key);
    }

    private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        //Console.WriteLine("Window_PreviewKeyDown, canvas1.IsFocused? {0}",
        //    canvas1.IsFocused);

        //if (canvas1.IsFocused)
        //    MoveCanvas(e.Key);
    }

    void MoveCanvas(Key key)
    {
        int delta = 10;
        switch (key)
        {
            case Key.Left: canvas1.Margin = new Thickness(
                canvas1.Margin.Left - delta,
                canvas1.Margin.Top,
                canvas1.Margin.Right,
                canvas1.Margin.Bottom);
                break;
            case Key.Up: canvas1.Margin = new Thickness(
                canvas1.Margin.Left,
                canvas1.Margin.Top - delta,
                canvas1.Margin.Right,
                canvas1.Margin.Bottom + delta);
                break;
            case Key.Right: canvas1.Margin = new Thickness(
                canvas1.Margin.Left,
                canvas1.Margin.Top,
                canvas1.Margin.Right - delta,
                canvas1.Margin.Bottom);
                break;
            case Key.Down: canvas1.Margin = new Thickness(
                canvas1.Margin.Left,
                canvas1.Margin.Top + delta,
                canvas1.Margin.Right,
                canvas1.Margin.Bottom - delta);
                break;
            default:
                break;
        }
    }
}

Bellow是鼠标按下控件后的执行日志和三个“keyUp”:

   canvas_MouseDown 0, canvas1.IsFocused? False ' before click '
   canvas_MouseDown 1, canvas1.IsFocused? True  ' after click ' 
Window_PreviewKeyDown, canvas1.IsFocused? True  ' up 1 '
canvas_PreviewKeyDown, canvas1.IsFocused? True  ' up 1 '
Window_PreviewKeyDown, canvas1.IsFocused? False ' up 2 '
Window_PreviewKeyDown, canvas1.IsFocused? False ' up 3 '

1 个答案:

答案 0 :(得分:1)

你可以尝试两件事:

  1. 如果你正在处理事件,请将e.Handled设置为true。这也可以防止其他事情对击键做出反应。

  2. 您可能需要在所有祖先控件上使用KeyboardNavigation.DirectionalNavigation设置。默认情况下,箭头键会移动键盘焦点,但这取决于DirectionalNavigation设置。你想把它设置为无,但假设#1没有解决问题。