启用和禁用按钮

阿兰392

我有一个绑定属性的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
阿亚潘(Ayyappan)

我添加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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章