1

我有一种情况,我正在验证一个用于启用按钮的文本框。如果文本框为空,则应禁用该按钮,反之亦然。如果我在 XAML 后面的代码中编写逻辑,我可以处理代码并实现解决方案,但我觉得那不是正确的方式,应该从 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 中编写以下代码来实现解决方案,但不确定要写什么。

public void ShowDoNotContact()
{
    Binding myBinding = new Binding("DNCNotes");

    //myBinding.Source =  DataContext as NextLeadWizardViewModel;

    myBinding.Source = txtDNCNotes;

    myBinding.Path = new PropertyPath("DNCNotes");
    myBinding.Mode = BindingMode.TwoWay;
    myBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
    BindingOperations.SetBinding(txtDNCNotes, TextBox.TextProperty, myBinding);

    if (_dncNotes == null)
        OkCommand.IsEnabled = false;
    else
        OkCommand.IsEnabled = CanEnableOk();

}
4

2 回答 2

3

如果您想验证TextBox会禁用按钮的 a ,我会使用 a 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 不应接受 null 或空值,显示在DCNoteFormCanExecute()(DCNotes 是您在 Viewmodel 中定义的属性)中。

并在 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-Applic

或者

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

于 2013-01-08T10:29:31.657 回答
1

ViewModel 是一个可接受的地方,可以为您的视图添加不影响您的模型的支持属性。例如,类似于以下内容:

    public bool DncCanExecute
    {
        get
        {
           return "" != _dncNotes;
        }
    }

    public string DNCNotes
    {
        get { return _dncNotes; }
        set { 
            if (_dncNotes == value) return;
            if (("" == _dncNotes && "" != value) || ("" != _dncNotes && "" == value))
            {
                _dncNotes = value;
                OnPropertyChanged("DncCanExecute");
            }
            else
            {
                _dncNotes = value;
            }
            OnPropertyChanged("DNCNotes");
        }
    }

从那里,您可以将属性绑定Button.IsEnabledDncCanExecute属性以获得所需的功能。

于 2013-01-07T17:44:07.307 回答