简体   繁体   English

WPF工具包ColorPicker消除焦点

[英]WPF Toolkit ColorPicker remove focus

I'm using the ColorPicker control from the Extended WPF Toolkit with some restrictions as I want the user to only have the ColorCanvas enabled. 我正在使用扩展WPF工具包中的ColorPicker控件,但有一些限制,因为我希望用户仅启用ColorCanvas

<xctk:ColorPicker Focusable="False" ColorMode="ColorCanvas" 
    ShowAdvancedButton="False" ShowDropDownButton="False" 
    ShowAvailableColors="False" SelectedColor="{Binding CustomAccentColor}" 
    ShowRecentColors="False" ShowStandardColors="False" />

When clicking the ToggleButton the canvas is shown and the user can pick the color he wants to. 当单击ToggleButton ,将显示画布,用户可以选择他想要的颜色。 However, when clicking outside the canvas closes as expected. 但是,在画布外部单击时会按预期关闭。

Problem now is, that when pressing space-bar, the canvas is opened again. 现在的问题是,按空格键时,画布将再次打开。 In the application it is important that nothing other than the containing Window can have focus so the space-bar pressing can be routed to a specific command. 在应用程序中,重要的是除包含Window以外的其他任何东西都不能聚焦,因此可以将空格键的按下传递给特定的命令。

I've already tried to set Focusable="False" , bind to the LostFocus -event and set the focus on the window in code-behind but nothing worked. 我已经尝试设置Focusable="False" ,绑定到LostFocus -event并将焦点设置在代码隐藏窗口中,但是没有任何效果。 The event-callback was never hit. 从未发生过事件回调。 I've also tried the SelectedColorChanged -event but this was also not working. 我也尝试了SelectedColorChanged -event,但这也没有用。

How I can force the focus away from the ColorPicker back to my Window ? 如何强制将焦点从ColorPicker移回我的Window

You will probably find that the LostFocus even is being handled on a lower control that actually has focus, thus you are not seeing it. 您可能会发现LostFocus甚至在实际上具有焦点的较低控件上进行处理,因此您看不到它。 What you can do is hook into the PreviewKeyDown event and add the following inside a check for whatever key you want to suppress: 您可以做的是挂接到PreviewKeyDown事件,然后在检查中添加以下内容以查找要隐藏的任何键:

   private void ColorPicker_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Space)
        {
            e.Handled = true;
            ColorPicker1.IsOpen = true;
        }
    }

This will override the behaviour handled in ColorPicker control by preventing it from receiving the KeyDown event. 这将通过阻止ColorPicker控件接收KeyDown事件来覆盖它的行为。 You can also move focus using something like: 您还可以使用以下方法移动焦点:

    private void ColorPicker_GotFocus(object sender, RoutedEventArgs e)
    {
        ColorPicker1.MoveFocus(new TraversalRequest(FocusNavigationDirection.Previous));
    }

This will ensure that mouse clicks move focus back to the previous focus control. 这将确保鼠标单击将焦点移回到上一个焦点控件。 It doesn't prevent and endless loop though so make sure it doesn't focus back to itself! 尽管它不会阻止并产生无限循环,所以请确保它不会将焦点转移回自身!

Finally, you may also wish to set IsTabStop and IsKeyboardFocus to false to prevent tabbing into the color picker if you do not wish it to have focus or be tabbed into. 最后,您可能还希望将IsTabStop和IsKeyboardFocus设置为false,以防止在不希望将其聚焦或跳入制表器时跳入颜色选择器。

Note: I am using a named control here but you should be able to get the control in question through the sender. 注意: 我在这里使用命名控件,但是您应该能够通过发件人获取有问题的控件。

This works for me: 这对我有用:

private void Selected_Color_Changed(object sender, RoutedPropertyChangedEventArgs<Color?> e)
{
    YourRichTextControlElement.Focus();

    Xceed.Wpf.Toolkit.ColorPicker colorPicker = sender as Xceed.Wpf.Toolkit.ColorPicker;

    colorPicker.IsOpen = false;
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM