0

ユーザーが保存のキーの組み合わせ (Ctrl-S) を押したときに、アクティブな TextBox の内容を ViewModel のバインドされたプロパティに書き戻したいと考えています。

私の問題は、バインドされた Text-Property が TextBox の内容を反映するように、バインディングの実行をトリガーできないことです。

・GetBindingメソッドがないようです。したがって、バインディングを取得して手動で実行することはできません。
- WinForms のようにバインディングを実行する Validate メソッドはありません -
KeyDown 内から別のコントロールにフォーカスを移しても動作しないようで、バインディングは実行されません

どうすればこれを達成できますか?

4

3 に答える 3

1

私はあなたの質問をよりよく理解していると思います。これを回避する1つの方法は、ここからのような新しいプロパティを持つサブクラス化されたテキストボックスを使用することです:

public class BindableTextBox : TextBox
{
    public string BindableText
    {
        get { return (string)GetValue(BindableTextProperty); }
        set { SetValue(BindableTextProperty, value); }
    }

    // Using a DependencyProperty as the backing store for BindableText.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty BindableTextProperty =
        DependencyProperty.Register("BindableText", typeof(string), typeof(BindableTextBox), new PropertyMetadata("", OnBindableTextChanged));

    private static void OnBindableTextChanged(DependencyObject sender, DependencyPropertyChangedEventArgs eventArgs)
    {
        ((BindableTextBox)sender).OnBindableTextChanged((string)eventArgs.OldValue, (string)eventArgs.NewValue);
    }

    public BindableTextBox()
    {
        TextChanged += BindableTextBox_TextChanged;
    }

    private void OnBindableTextChanged(string oldValue, string newValue)
    {
        Text = newValue ? ? string.Empty; // null is not allowed as value!
    }

    private void BindableTextBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        BindableText = Text;
    }    
}

次に、BindableTextプロパティにバインドします。

于 2012-09-04T20:33:37.613 に答える
1

WiredPrarie のブログ投稿 ( http://www.wiredprarie.us/blog/index.php/archives/1701 ) で、これに関する Aaron の議論をご覧ください。

于 2012-09-04T15:56:47.687 に答える
0

コマンドインスタンス
の解決策ここで私が見つけた解決策は、比較的軽量ですが、少し「ハック」でもあります。

btn.Focus(Windows.UI.Xaml.FocusState.Programmatic);
Dispatcher.ProcessEvent(CoreProcessEventsOption.ProcessAllIfPresent);         
btn.Command.Execute(null);

まず、別のコントロール(私の場合はバインドされたコマンドを持つボタン)にフォーカスを置きます。次に、システムにバインディングを実行する時間を与え、最後にボタンにバインドされているコマンドを起動します。

バインドされたコマンドを使用しないソリューション
別のコントロールにフォーカスを与え、Dispatcher.ProcessEvent(...)を呼び出します。

anotherControl.Focus(Windows.UI.Xaml.FocusState.Programmatic);
Dispatcher.ProcessEvent(CoreProcessEventsOption.ProcessAllIfPresent);
// Do your action here, the bound Text-property (or every other bound property) is now ready, binding has been executed

BStatehamのソリューションも参照してください
それは問題を解決する別の方法です

于 2012-09-04T17:51:07.253 に答える