简体   繁体   English

设置RadNumericBox Focus Telerik WPF的属性

[英]Property to Set RadNumericBox Focus Telerik WPF

I am trying to set Focus to RadNumericBox Telerik Control. 我正在尝试将Focus设置为RadNumericBox Telerik Control。 There is no property for the control to set the Focus values. 控件没有属性来设置Focus值。

Although, I can do it in my code behind by getting the VisualChild of NumericTextBox and setting the Focus values. 虽然,我可以通过获取NumericTextBox的VisualChild并设置Focus值在后面的代码中进行操作。

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

But I need to change the Focus in the Viewmodel. 但是我需要在Viewmodel中更改Focus。 So I am looking for a property, so that viewmodel can be binded to it. 所以我正在寻找一个属性,以便可以将viewmodel绑定到它。

In general to manipulate focus and other such visual procedures, I would recommend you to create a custom Attached Property on UIElement and then when that property is set you can set focus to UIElement 通常,为了操纵焦点和其他此类视觉过程,我建议您在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();
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM