【问题标题】:Property to Set RadNumericBox Focus Telerik WPF设置 RadNumericBox 焦点 Telerik WPF 的属性
【发布时间】:2016-02-15 08:43:39
【问题描述】:

我正在尝试将焦点设置为 RadNumericBox Telerik Control。控件没有用于设置焦点值的属性。

虽然,我可以在后面的代码中通过获取 NumericTextBox 的 VisualChild 并设置焦点值来实现。

    NumericTextBox box1 = VisualTreeUtilities.GetVisualChild<NumericTextBox>(RadNumericBox1);
    box1.Focus(FocusState.Programmatic);

但我需要更改 Viewmodel 中的焦点。所以我正在寻找一个属性,以便可以将 viewmodel 绑定到它。

【问题讨论】:

    标签: c# wpf telerik


    【解决方案1】:

    一般来说,为了操作焦点和其他此类可视化程序,我建议您在 UIElement 上创建一个自定义附加属性,然后在设置该属性后,您可以将焦点设置为 UIElement

        public static class FocusExtension
        {
    
            public static bool GetFocused(DependencyObject obj)
            {
                return (bool)obj.GetValue(FocusedProperty);
            }
    
    
            public static void SetFocused(DependencyObject obj, bool value)
            {
                obj.SetValue(FocusedProperty, value);
            }
    
    
            public static readonly DependencyProperty FocusedProperty =
                   DependencyProperty.RegisterAttached("Focused",
                   typeof(bool), typeof(FocusExtension),
                   new UIPropertyMetadata(false, OnFocusedPropertyChanged));
    
            private static void OnFocusedPropertyChanged(DependencyObject d,
                DependencyPropertyChangedEventArgs e)
            {
                var uiElement = (UIElement)d;
                var toSet = (bool)e.NewValue;
                if (toSet)
                {
                    uiElement.Focus();
                }
            }
        }
    

    【讨论】:

    • 非常感谢您的回复!
    • 好的。这行得通。但如果我将 FocusExtension 重命名为 RadNumericBoxFocusExtension,我的 xaml 会显示错误。我在 DependencyProperty 中也更改了 typeof(RadNumericBoxFocusExtension),我错过了什么吗?
    • 这是什么错误,你是如何使用这个扩展的?
    猜你喜欢
    • 2011-09-17
    • 2010-11-26
    • 1970-01-01
    • 2016-01-19
    • 1970-01-01
    • 2012-04-23
    • 2011-06-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多