Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 当用户控件可见时,将其设置为焦点_Wpf_Mvvm_User Controls_Command_Focus - Fatal编程技术网

Wpf 当用户控件可见时,将其设置为焦点

Wpf 当用户控件可见时,将其设置为焦点,wpf,mvvm,user-controls,command,focus,Wpf,Mvvm,User Controls,Command,Focus,我正在显示一个消息框,希望用户能够使用CTRL+C复制消息内容。问题是我似乎无法将焦点设置到对话框 MessageBox在MVVM中实现。为了显示它,我只需将usercontrol设置为可见(中央屏幕)并禁用主视图 copy命令使用Prism DelegateCommand实现: <UserControl.InputBindings> <KeyBinding Key="C" Modifiers="Control" Command="{Binding CopyComman

我正在显示一个
消息框
,希望用户能够使用CTRL+C复制消息内容。问题是我似乎无法将焦点设置到对话框

MessageBox
在MVVM中实现。为了显示它,我只需将usercontrol设置为可见(中央屏幕)并禁用主视图

copy命令使用Prism DelegateCommand实现:

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

如果我在消息框按钮上单击tab键,则会触发copy命令。但是,当显示对话框时,我无法使其开始工作

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


注意:
我需要一个MVVM解决方案,因为我不希望代码隐藏文件中有任何代码。

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

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

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

可视焦点行为

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
}
<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>
使用示例

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
}
<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>
代码隐藏

private void Button_Click(object sender, RoutedEventArgs e)
{
    TestUserControl.Visibility = Visibility.Visible;
}
完整的例子可以在这里找到


有一种简单的方法可以设置控件的焦点。下面是一个示例:

<UserControl
         FocusManager.FocusedElement="{Binding ElementName=txtNickname}">
<Grid>
    <Grid HorizontalAlignment="Center" VerticalAlignment="Center">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <TextBox x:Name="txtNickname" Grid.Row="1" />
    </Grid>
</Grid>

谢谢分享。有太多的解决方案,只有在控件已经可见时才有效,或者在这种情况下会间歇性失败。