属性 设置 RadNumericBox 焦点 Telerik WPF

Property to Set RadNumericBox Focus Telerik WPF

我正在尝试将焦点设置为 RadNumericBox Telerik 控件。控件没有 属性 来设置 Focus 值。

不过,我可以通过获取 NumericTextBox 的 VisualChild 并设置 Focus 值在我的代码中完成此操作。

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

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

一般来说,要操纵焦点和其他此类可视化程序,我建议您在 UIElement 上创建一个自定义的 Attached 属性,然后在设置 属性 时,您可以设置关注 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();
            }
        }
    }