簡體   English   中英

設置RadNumericBox Focus Telerik WPF的屬性

[英]Property to Set RadNumericBox Focus Telerik WPF

我正在嘗試將Focus設置為RadNumericBox Telerik Control。 控件沒有屬性來設置Focus值。

雖然,我可以通過獲取NumericTextBox的VisualChild並設置Focus值在后面的代碼中進行操作。

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

但是我需要在Viewmodel中更改Focus。 所以我正在尋找一個屬性,以便可以將viewmodel綁定到它。

通常,為了操縱焦點和其他此類視覺過程,我建議您在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