通过焦点管理器在xaml中聚焦文本

时间:2014-10-23 13:10:15

标签: c# wpf xaml focus

我有一个WPF窗口,当窗口通过FocusManager启动时,它会聚焦文本框。 e.g。

<Window 
   ...
   FocusManager.FocusedElement="{Binding ElementName=nameTextBox}">    

关注焦点后,我还想突出显示文本框中的文字。如果可能的话,我想在XAML中完成所有操作,因为我没有代码背后的代码,并希望保持这种方式。

1 个答案:

答案 0 :(得分:0)

我知道你想要将.cs从你的项目中删除但是它很难实现,你可以创建一个带有附加属性的一个类,它可以在你的xaml文件中反复使用。 / p>

  public class AutoFocusBehavior : DependencyObject
{
    public static bool IsAutoFocus(DependencyObject obj)
    {
        return (bool)obj.GetValue(IsAutoFocusProperty);
    }
    public static void SetIsAutoFocus(DependencyObject obj, bool value)
    {
        obj.SetValue(IsAutoFocusProperty, value);
    }
    public static readonly DependencyProperty IsAutoFocusProperty =
        DependencyProperty.RegisterAttached("IsAutoFocus", typeof(bool), typeof(AutoFocusBehavior),
            new PropertyMetadata(false, new PropertyChangedCallback((d, de) =>
            {
                if ((bool)de.NewValue)
                {
                    FrameworkElement frameworkElement = (FrameworkElement)d;
                    frameworkElement.Unloaded += frameworkElement_Unloaded;
                    frameworkElement.IsVisibleChanged += new DependencyPropertyChangedEventHandler(frameworkElement_IsVisibleChanged);
                    frameworkElement.IsEnabledChanged += frameworkElement_IsEnabledChanged;
                }
                else
                {
                    FrameworkElement frameworkElement = (FrameworkElement)d;
                    frameworkElement.Unloaded -= frameworkElement_Unloaded;
                    frameworkElement.IsVisibleChanged -= new DependencyPropertyChangedEventHandler(frameworkElement_IsVisibleChanged);
                    frameworkElement.IsEnabledChanged -= frameworkElement_IsEnabledChanged;
                }
            })));
    static void frameworkElement_Unloaded(object sender, RoutedEventArgs e)
    {
        FrameworkElement frameworkElement = (FrameworkElement)sender;
        frameworkElement.IsVisibleChanged -= new DependencyPropertyChangedEventHandler(frameworkElement_IsVisibleChanged);
        frameworkElement.IsEnabledChanged -= frameworkElement_IsEnabledChanged;
    }
    static void frameworkElement_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        if (((bool)e.NewValue))
        {
            if (sender is System.Windows.Controls.TextBox)
            {
                System.Windows.Controls.TextBox textBox = sender as System.Windows.Controls.TextBox;
                if (textBox != null)
                {
                    textBox.SelectAll();
                }
            }

            ((FrameworkElement)sender).Focus();
        }
    }
    static void frameworkElement_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        if (((bool)e.NewValue))
        {
            if (sender is System.Windows.Controls.TextBox)
            {
                System.Windows.Controls.TextBox textBox = sender as System.Windows.Controls.TextBox;
                if (textBox != null)
                {
                    textBox.SelectAll();
                    textBox.Focus();
                }

            }
            ((FrameworkElement)sender).Focus();
        }
    }
}

然后在任何类似的xaml中使用。

<Window x:Class="WpfApplication3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication3"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TextBox Text="testing" local:AutoFocusBehavior.IsAutoFocus="True" Margin="133,87,292,198"/>
    </Grid>
</Window>

enter image description here