-1

CRUDフォームで少し苦労しています。フォームを保存するボタンがあり、IsDefault フラグが true に設定されているため、ユーザーはいつでも Enter キーを押してフォームを保存できます。

問題は、ユーザーがテキスト ボックスに入力して Enter キーを押したときに、テキスト ボックス バインディングのソースが更新されないことです。これは、テキストボックスのデフォルトのUpdateSourceTrigger機能が であることが原因であることがわかっています。これはLostFocus、問題を解決するために使用した場合もありますが、実際には、他の場合により多くの問題を引き起こします。

標準stringフィールドの場合はこれで問題ありませんが、doubles やints などの場合はプロパティの変更時に検証が行われるため、ユーザーが1.5ダブル ソースにバインドされたテキスト ボックスに入力するのを停止します (1 を入力できますが、検証により、 10 進数の場合は、入力15してからカーソルを戻して押すこと.もできます)。

これにアプローチするより良い方法はありますか?PropertyChangedイベントを発生させることを思いついたコードでウィンドウ内のすべてのバインディングを更新する方法を調べましたがstring.empty、これはソースではなくターゲットのみを更新します。

4

2 に答える 2

2

バインディングに を設定したくない場合の私の標準的な解決策は、true に設定するとが押されたときにバインディングのソースを更新するUpdateSourceTrigger=PropertyChangedカスタムを使用することです。AttachedPropertyEnter

これが私の添付プロパティのコピーです

// When set to True, Enter Key will update Source
#region EnterUpdatesTextSource DependencyProperty

// Property to determine if the Enter key should update the source. Default is False
public static readonly DependencyProperty EnterUpdatesTextSourceProperty =
    DependencyProperty.RegisterAttached("EnterUpdatesTextSource", typeof (bool),
                                        typeof (TextBoxHelper),
                                        new PropertyMetadata(false, EnterUpdatesTextSourcePropertyChanged));

// Get
public static bool GetEnterUpdatesTextSource(DependencyObject obj)
{
    return (bool) obj.GetValue(EnterUpdatesTextSourceProperty);
}

// Set
public static void SetEnterUpdatesTextSource(DependencyObject obj, bool value)
{
    obj.SetValue(EnterUpdatesTextSourceProperty, value);
}

// Changed Event - Attach PreviewKeyDown handler
private static void EnterUpdatesTextSourcePropertyChanged(DependencyObject obj,
                                                          DependencyPropertyChangedEventArgs e)
{
    var sender = obj as UIElement;
    if (obj != null)
    {
        if ((bool) e.NewValue)
        {
            sender.PreviewKeyDown += OnPreviewKeyDownUpdateSourceIfEnter;
        }
        else
        {
            sender.PreviewKeyDown -= OnPreviewKeyDownUpdateSourceIfEnter;
        }
    }
}

// If key being pressed is the Enter key, and EnterUpdatesTextSource is set to true, then update source for Text property
private static void OnPreviewKeyDownUpdateSourceIfEnter(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Enter)
    {
        if (GetEnterUpdatesTextSource((DependencyObject) sender))
        {
            var obj = sender as UIElement;
            BindingExpression textBinding = BindingOperations.GetBindingExpression(
                obj, TextBox.TextProperty);

            if (textBinding != null)
                textBinding.UpdateSource();
        }
    }
}

#endregion //EnterUpdatesTextSource DependencyProperty

そして、次のように使用されます。

<TextBox Text="{Binding SomeText}" local:EnterUpdatesTextSource="True" />
于 2013-05-15T12:34:36.457 に答える
0

次のコードを使用してバインディング ソースを更新できます。

textBox1.GetBindingExpression(TextBox.TextProperty).UpdateSource();
于 2013-05-15T12:25:32.573 に答える