当焦点可见时,将焦点设置为usercontrol

问题描述 投票:4回答:1

我正在展示MessageBox,并希望用户能够使用CTRL + C复制邮件的内容。问题是我似乎无法将焦点设置到对话框中。

MessageBox在MVVM中实现。为了显示它我只是使用户控件可见(中心屏幕)并禁用主视图。

复制命令使用Prism DelegateCommand实现:

<UserControl.InputBindings>
    <KeyBinding Key="C" Modifiers="Control" Command="{Binding CopyCommand}"/>
</UserControl.InputBindings>

如果我选中了消息框按钮,则会触发CopyCommand。但是,当显示对话框时,我最初无法使其工作。

如何让usercontrol接受焦点或将KeyBinding附加到整个usercontrol?

Note:我需要一个MVVM解决方案,因为不需要代码隐藏文件中的任何代码。

wpf mvvm user-controls command focus
1个回答
4
投票

在使用MVVM模式并需要与用户界面交互的情况下,我总是尝试通过附加行为来实现此解决方案。附加行为是非常强大且方便的解决方案,完全满足MVVM模式,也可以在Blend中使用(具有预定义的接口)。

在这种情况下,我创建了一个附加行为VisibleFocusBehavior,它设置了一个IsVisibleChanged事件处理程序,其中焦点是在元素可见性的情况下设置的。

为了避免虚线框的出现,当控件获得焦点时,我为UserControl设置FocusVisualStyle="{x:Null}

VisibleFocusBehavior

public class VisibleFocusBehavior
{
    #region IsFocusEnabled Dependency Property

    public static readonly DependencyProperty IsFocusEnabledProperty;

    public static void SetIsFocusEnabled(DependencyObject DepObject, bool value)
    {
        DepObject.SetValue(IsFocusEnabledProperty, value);
    }

    public static bool GetIsFocusEnabled(DependencyObject DepObject)
    {
        return (bool)DepObject.GetValue(IsFocusEnabledProperty);
    }

    #endregion

    #region BringToFrontBehavior Constructor

    static VisibleFocusBehavior()
    {
        IsFocusEnabledProperty = DependencyProperty.RegisterAttached("IsFocusEnabled",
                                                            typeof(bool),
                                                            typeof(VisibleFocusBehavior),
                                                            new UIPropertyMetadata(false, IsFocusTurn));
    }

    #endregion

    #region IsFocusTurn

    private static void IsFocusTurn(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    {
        UIElement element = sender as UIElement;

        if (e.NewValue is bool && ((bool)e.NewValue) == true)
        {
            if (element != null)
            {
                element.IsVisibleChanged += new DependencyPropertyChangedEventHandler(ElementIsVisibleChanged);
            }
        }
    }

    #endregion

    #region ElementIsVisibleChanged Handler

    private static void ElementIsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e) 
    {
        UIElement visibilityElement = sender as UIElement;

        if (visibilityElement.IsVisible == true) 
        {
            visibilityElement.Focus();
        }
    }

    #endregion
}

Example of using

<UserControl x:Class="UserControlFocusHelp.TestUserControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:UserControlFocusHelp"             
         mc:Ignorable="d" 
         d:DesignHeight="300" 
         d:DesignWidth="300"
         xmlns:AttachedBehaviors="clr-namespace:UserControlFocusHelp.AttachedBehaviors"
         AttachedBehaviors:VisibleFocusBehavior.IsFocusEnabled="True"
         FocusVisualStyle="{x:Null}">

<UserControl.InputBindings>
    <KeyBinding Key="C" 
                Modifiers="Control"
                Command="{Binding CopyCommand}" />
</UserControl.InputBindings>

Test window

XAML

<Grid>
    <local:TestUserControl x:Name="TestUserControl"
                           Width="300"
                           Height="300"
                           Focusable="True"
                           Visibility="Collapsed" />

    <Button Width="100"
            Height="30" 
            Content="Visible" 
            HorizontalAlignment="Left"
            Click="Button_Click" />
</Grid>

Code-behind

private void Button_Click(object sender, RoutedEventArgs e)
{
    TestUserControl.Visibility = Visibility.Visible;
}

这个link提供了完整的示例。

© www.soinside.com 2019 - 2024. All rights reserved.