2013-01-07 47 views
1

我有一种情况,我正在验证用于启用按钮的文本框。如果文本框为空,则该按钮应该被禁用,反之亦然。如果我在XAML后面的代码中编写逻辑,我可以处理代码并实现解决方案,但我认为这不是正确的方式,应该从viewModel而不是后面的代码处理事件。ViewModel中的文本框事件处理

这里是我做了什么:
XAML

<TextBox Grid.Row="1" Margin="6,192,264,0" Height="60" VerticalAlignment="Top" 
     x:Name="txtDNCNotes" Text="{Binding Path=DNCNotes, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" 
     TextWrapping="Wrap" VerticalScrollBarVisibility="Auto" 
     Visibility="{Binding Path=DNCNoteTxtVisibility}" Grid.Column="1" 
     behaviour:TextBoxFilters.IsBoundOnChange="True" 
     TextChanged="TextBox_TextChanged" /> 


视图模型

public string DNCNotes 
{ 
    get { return _dncNotes; } 
    set { 
     if (_dncNotes == value) return; 
     _dncNotes = value; 
     OnPropertyChanged("DNCNotes"); 
    } 
} 


代码背后

private void TextBox_TextChanged(object sender, TextChangedEventArgs e) 
{ 
    var ctx = LayoutRoot.DataContext as NextLeadWizardViewModel; 
    BindingExpression binding = txtDNCNotes.GetBindingExpression(TextBox.TextProperty).UpdateSource(); 
    ctx.ShowDoNotContact(); 
}  

我想在viewModel中编写下面的代码来实现解决方案,但不知道要写什么。

​​
+2

不知道如果我得到你的权利......“我有一个情况,我在哪里验证用于启用按钮一个文本框”。使用命令的CanExecute? – Lucas

+0

嗨,感谢您的建议,我的意思是我想在ViewModel中编写验证,而不是代码。我从来没有想过CanExecute,但我会去探索它。 – DotNetGeek

回答

3

如果要验证将禁用按钮的TextBox,我会使用command,类似于此;

private ICommand showDCNoteCommand; 
    public ICommand ShowDCNoteCommand 
    { 
     get 
     { 
      if (this.showDCNoteCommand == null) 
      { 
       this.showDCNoteCommand = new RelayCommand(this.DCNoteFormExecute, this.DCNoteFormCanExecute); 
      } 

      return this.showDCNoteCommand; 
     } 
    } 

    private bool DCNoteFormCanExecute() 
    { 
     return !string.IsNullOrEmpty(DCNotes); 

    } 

    private void DCNoteFormExecute() 
    { 
     DCNoteMethod(); //This a method that changed the text 
    } 

这将确保用户无法继续,或保存进步作为TextBox不能接受空或空值,内所示的DCNoteFormCanExecute()(该DCNotes是,你有你的视图模型中定义的属性)。

并在xaml中,像这样绑定到按钮;

<Button Content="Save" Grid.Column="1" Grid.Row="20" x:Name="btnSave" VerticalAlignment="Bottom" Width="75" Command="{Binding ShowDCNoteCommand}" 

进行验证,你可以做一些简单的像这样,使用属性验证,使用该参考using System.ComponentModel.DataAnnotations;

[Required(ErrorMessage = "DCNotes is required")] 
    [RegularExpression(@"^[a-zA-Z''-'\s]{1,5}$", ErrorMessage = "DCNotes must contain no more then 5 characters")] //You can change the length of the property to meet the DCNotes needs 
    public string DCNotes 
    { 
     get { return _DCNotes; } 
     set 
     { 
      if (_DCNotes == value) 
       return; 

      _DCNotes = value; 
      OnPropertyChanged("DCNotes"); 
     } 
    } 

和XAML中,你可以创建一个Resource突出框通知未填写文本框的用户;

<Style TargetType="{x:Type TextBlock}"> 
     <Setter Property="Margin" 
       Value="4" /> 
    </Style> 

    <Style TargetType="{x:Type TextBox}"> 
     <Setter Property="Margin" 
       Value="4" /> 
     <Style.Triggers> 
      <Trigger Property="Validation.HasError" 
        Value="true"> 
       <Setter Property="ToolTip" 
         Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"/> 

      </Trigger> 
     </Style.Triggers> 
    </Style> 

我希望这会有帮助,否则,这里的链接可能会有所帮助; http://www.codeproject.com/Articles/97564/Attributes-based-Validation-in-a-WPF-MVVM-Applicat

OR

http://www.codearsenal.net/2012/06/wpf-textbox-validation-idataerrorinfo.html#.UOv01G_Za0t

+0

你能否建议