1

ConvertとConvertBackを実装するコンバーターを使用してDateTimeにバインドされたTextBoxがあります。UpdateSourceTriggerはPropertyChangedに設定されているため、ユーザーが入力したときに検証が行われます。ここに問題があります:

  1. ユーザーが文字を入力する
  2. ConverterはこれをDateTimeに解析します
  3. ViewModelのプロパティが更新され、検証されます
  4. 次に、ConverterはこのDateTimeを文字列に変換し直し、テキストボックスの文字列を変更します

ユーザーが日付の一部しか入力していないときにテキストが完全な日付に変更される可能性があるため、これは望ましくありません。UIがこれを実行しないようにするにはどうすればよいですか?この機能のために、.NET 3.5から4.0にアップグレードしてから、これが問題になっているだけであることに注意してください。

http://karlshifflett.wordpress.com/2009/05/27/wpf-4-0-data-binding-change-great-feature/

助けてくれてありがとう!

4

2 に答える 2

1

データ検証を使用すると、プロパティが型付き値を取得する前に、型付き値が特定の条件(正規表現など)を満たしているかどうかを確認できます->コンバーターは、条件が満たされた場合にのみ呼び出されます。
別の提案は、入力された値がDateTimeである場合は正規表現を介してコンバーターをチェックインし、一致する場合にのみ変換することです。

于 2012-10-30T12:38:36.983 に答える
0

これを試して:

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    'Limit the length of the number that can be entered to 12 (This is arbitrary)
    If Len(TextBox1.Text) > 12 Then
        KeyAscii = 0
        Exit Sub
    End If

    If KeyAscii < 32 Then
        Exit Sub    ' let it go, it is a control char like backspace
    End If

    If InStr("0123456789.", Chr$(KeyAscii)) > 0 Then
        If InStr(TextBox1.Text, ".") > 0 Then
            If Chr$(KeyAscii) = "." Then
                KeyAscii = 0    ' do not allow more than one decimal point
                Beep
                MsgBox "Only 2 decimal places to be allowed", vbCritical
                Exit Sub

            ElseIf Len(Mid$(TextBox1.Text, InStr(TextBox1.Text, ".") + 1)) >= 2 Then
                KeyAscii = 0    ' do not allow more than 2 digits past decimal point
                Beep
                MsgBox "Only 2 decimal places to be allowed", vbCritical
                Exit Sub

            End If
        End If
    Else
        Beep
        KeyAscii = 0
    End If

    If Len(Mid$(TextBox1.Text, InStr(TextBox1.Text, "$") + 1)) >= 1 Or KeyAscii < 32 Then
        If Len(Mid$(TextBox1.Text, InStr(TextBox1.Text, ".") + 1)) >= 2 Then

            If KeyAscii < 32 Then Beep: Exit Sub
            TextBox1.Text = Format(TextBox1.Text, "$#,###")
        Else
            If KeyAscii < 32 Then Beep: Exit Sub
            TextBox1.Text = TextBox1.Text
        End If
    ElseIf Len(Mid$(TextBox1.Text, InStr(TextBox1.Text, "$") + 1)) >= 0 Or KeyAscii < 32 Then
        If Len(Mid$(TextBox1.Text, InStr(TextBox1.Text, ".") + 1)) >= 2 Then

            If KeyAscii < 32 Then Beep: Exit Sub
            TextBox1.Text = Format(TextBox1.Text, "$#,###")
        Else
            If KeyAscii < 32 Then Beep: Exit Sub
            TextBox1.Text = ""
            TextBox1.Text = "$" & TextBox1.Text
        End If
    End If


End Sub
Private Sub TextBox1_Change()

    If Len(Mid$(TextBox1.Text, InStr(TextBox1.Text, ".") + 1)) >= 3 Then
        TextBox1.Value = Format(PaidCreditCardForfrm.TextBox1.Value, "$#,###")
    End If

End Sub
于 2015-01-01T02:12:20.993 に答える