【问题标题】:Focusing text through focus manager in xaml通过 xaml 中的焦点管理器聚焦文本
【发布时间】:2014-10-23 13:10:14
【问题描述】:

我有一个 WPF 窗口,当窗口通过 FocusManager 启动时,它会聚焦一个文本框,它可以工作。例如

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

聚焦后,我还想突出显示文本框中的文本。如果可能,我想在 XAML 中完成这一切,因为我在代码中没有代码并且希望保持这种方式。

【问题讨论】:

  • 这在后面的代码中更容易完成,尤其是在文本框绑定到某个值时,因为要选择的文本在处理 Xaml 时不可用。

标签: c# wpf xaml focus


【解决方案1】:

我知道您希望将 .cs 排除在您的项目之外,但这很难实现,您可以创建一个带有附加属性的一次性类,该属性可以在您的 xaml 文件中反复使用。

  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>

【讨论】:

    猜你喜欢
    • 2013-03-24
    • 2023-03-26
    • 1970-01-01
    • 2017-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-06
    • 2011-02-06
    相关资源
    最近更新 更多