一般操作對焦等這樣的可視化程序,我建議你上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();
}
}
}
感謝很多的回覆! – Mullaly
好的。這工作。但是,如果我將FocusExtension重命名爲RadNumericBoxFocusExtension,那麼我的xaml會顯示一個錯誤。我改變了DependencyProperty也爲typeof(RadNumericBoxFocusExtension), 我錯過了什麼? – Mullaly
它是什麼錯誤,你如何使用這個擴展名? – Muds