我有一个与属性绑定的texbox。
<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
当我输入一些字符或文本框为空时,不能启用按钮 Cmd_Insert ,但不起作用。 为什么? (参见函数 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
答案 0 :(得分:0)
我添加了TargetNullValue=''
并将您的属性更改为可以为空。请参阅以下代码。
<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