我有一個TextBox,我將焦點放在使用綁定到視圖模型屬性的附加屬性上。附加屬性調用「UIElement.Focus()」來設置焦點。問題是當TextBox以這種方式接收焦點時,「GotFocus」事件不會觸發。我正在使用Caliburn.Micro的Message.Attach來處理事件。有任何想法嗎?當文本框獲取焦點時需要校準Message.Attach的幫助
這是TextBox。
<TextBox x:Name="Test"
Grid.Column="0"
Text="{Binding Test, Converter={StaticResource TestToStringConverter}}"
AttachedProperties:FocusExtension.IsFocused="{Binding IsTestFocused}"
cal:Message.Attach="[Event GotFocus] = [Action OnGotFocus($eventargs)]; />
這裏是附加屬性(在SO上找到)。
public static class FocusExtension
{
public static bool GetIsFocused(DependencyObject obj)
{
return (bool) obj.GetValue(IsFocusedProperty);
}
public static void SetIsFocused(DependencyObject obj, bool value)
{
obj.SetValue(IsFocusedProperty, value);
}
public static readonly DependencyProperty IsFocusedProperty =
DependencyProperty.RegisterAttached("IsFocused", typeof (bool), typeof (FocusExtension),
new UIPropertyMetadata(false, OnIsFocusedPropertyChanged));
private static void OnIsFocusedPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var uie = (UIElement)d;
if ((bool)e.NewValue)
{
uie.Focus();
}
}
}
你能否提供附屬物的代碼? – devdigital 2011-03-17 23:48:15
已更新,以添加附加的財產代碼。 – Snarfblatt 2011-03-18 14:53:13