[英]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.