当用户控件可见时将焦点设置到它

2024-05-02

I am showing a MessageBox and want the user to be able to copy the contents of the message using CTRL+C. The problem is that I can't seem to set focus to the dialog.

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

复制命令是使用 Prism DelegateCommand 实现的:

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

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

如何让用户控件接受焦点或将 KeyBinding 附加到整个用户控件?

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


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

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

为了避免出现虚线框,当控件获得焦点时,我设置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 https://skydrive.live.com/redir?resid=3FC95A33B3F2DE8A!120.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

当用户控件可见时将焦点设置到它 的相关文章

随机推荐