0

ビューモデルのプロパティにバインドされた添付プロパティを使用してフォーカスを設定している TextBox があります。添付プロパティは「UIElement.Focus()」を呼び出してフォーカスを設定します。問題は、TextBox がこの方法でフォーカスを受け取ると、"GotFocus" イベントが発生しないことです。イベントを処理するために Caliburn.Micro の Message.Attach を使用しています。何か案は?

ここにテキストボックスがあります。

<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();
        }
    }
}
4

1 に答える 1

0

私はこれを自分で試しましたが、問題を再現することができました。なぜこれが起こるのかよくわかりません。ユーザー コントロール (つまり、ビュー) のライフサイクルに関係している可能性があります。1 つのオプションとして、添付プロパティを拡張して、ビュー モデルで を呼び出した時点で動詞を呼び出すことができますuie.Focus()

FocusExtension動詞の名前は、添付プロパティの依存関係プロパティである可能性があり、ビューで設定できます。

于 2011-03-21T23:02:33.627 に答える