2015-11-05 216 views
0

我有一個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

http://stackoverflow.com/questions/19364364/mvvm-relaycommand-canexecute 它在C# – Eldho

+0

沒有,爲Vb ...... – Alan392

+0

嘗試設置'在你的綁定模式= TwoWay' – Tomtom

回答

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 
+0

對不起..但TargetNullValue在哪裏?我沒有找到 – Alan392

+0

編輯我的答案。它的約束力。 –

+0

太棒了! ;-)謝謝你的幫助! – Alan392