0

I have a texbox in binding with a property.

 <TextBox Name="txtPrice"   Grid.Row="0"  Grid.Column="2" MaxLength="8" TabIndex="1"
   Text="{Binding Price, UpdateSourceTrigger=PropertyChanged, ValidatesOnExceptions=True, 
   StringFormat= '\{0:#,###.##\}', ConverterCulture=fr-FR}" TextWrapping="Wrap"/>


      Private Property _Price As Double
        Public Property Price As Double
        Get
            Return Price
        End Get
        Set(value As Double)
            _Price = Double.Parse(value)
            OnPropertyChanged("Price")
        End Set
    End Property

When I type some chars or the textbox is empty, the button Cmd_Insert must not be enabled, but doesn't work. Why ? (see Function CanCmd_Insert())

    Public ReadOnly Property Cmd_Insert As ICommand
    Get
        If _Cm_Insert Is Nothing Then
            _Cm_Insert = New RelayCommand(AddressOf Cmd_InsertExe, AddressOf CanCmd_Insert)
        End If
        Return _Cm_Insert
    End Get
End Property
Private Sub Cmd_InsertExe()
    UPDATE_Price()
End Sub
Private Function CanCmd_Insert() As Boolean
    If IsNumeric(Price) = False Then
        Return False
    Else
        Return True
    End If
End Function
4
  • stackoverflow.com/questions/19364364/… Its in C# Commented Nov 5, 2015 at 14:31
  • Try to set Mode=TwoWay in your binding Commented Nov 5, 2015 at 14:45
  • I have done, but doens't work ... Commented Nov 5, 2015 at 14:50
  • This is the issue. If I type a number, and after I delete it with backspace, the property Price has old value and the button is enabled yet. Commented Nov 5, 2015 at 14:57

1 Answer 1

1

I added TargetNullValue='' and changed your property to nullable. Refer the below code.

  <StackPanel>
            <TextBox Name="txtPrice"   Grid.Row="0"  Grid.Column="2" MaxLength="8" TabIndex="1"
   Text="{Binding Price, UpdateSourceTrigger=PropertyChanged, TargetNullValue='',
   StringFormat= '\{0:#,###.##\}'}" TextWrapping="Wrap" />            
            <Button Content="Update" Command="{Binding Cmd_Insert }"></Button>
        </StackPanel>

    Imports GalaSoft.MvvmLight.CommandWpf
Imports System.ComponentModel

Public Class ViewModel
    Implements INotifyPropertyChanged
    Private Property _Price As Double?
    Public Property Price As Double?
        Get
            Return _Price
        End Get
        Set(value As Double?)
            _Price = value
            OnPropertyChanged("Price")
        End Set
    End Property

    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

    Private Sub OnPropertyChanged(ByVal info As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
    End Sub
    Private Property _Cm_Insert As ICommand
    Public ReadOnly Property Cmd_Insert As ICommand
        Get
            If _Cm_Insert Is Nothing Then
                _Cm_Insert = New RelayCommand(AddressOf Cmd_InsertExe, AddressOf CanCmd_Insert)
            End If
            Return _Cm_Insert
        End Get
    End Property
    Private Sub Cmd_InsertExe()

    End Sub
    Private Function CanCmd_Insert() As Boolean
        If IsNumeric(Price) = False Then
            Return False
        Else
            Return True
        End If
    End Function
End Class
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry .. but where is the TargetNullValue ? I didn't found
Edited my answer. Its in binding.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.