【问题标题】:WPF MVVM IDataErrorInfoWPF MVVM IDataErrorInfo
【发布时间】:2016-03-30 17:41:45
【问题描述】:

我有一个 MVVM 模式的应用程序;它包含一个文本框和一个按钮;

我添加了一个验证,如果文本框为空,文本框颜色变为红色;

此时我希望按钮启用直到用户为假

在文本框中输入字符,然后按钮启用更改为真;

我的 XAML 代码是:

    <Window.Resources>
    <ControlTemplate x:Key="ErrorTemplate">
        <DockPanel LastChildFill="True">
            <Border BorderBrush="Pink" BorderThickness="1">
                <AdornedElementPlaceholder />
            </Border>
        </DockPanel>
    </ControlTemplate>
    <Style TargetType="TextBox">
        <Style.Triggers>
            <Trigger Property="Validation.HasError" Value="true">
                <Setter Property="ToolTip"
      Value="{Binding RelativeSource={x:Static RelativeSource.Self},Path=(Validation.Errors)[0].ErrorContent}"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>


<Grid>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <TextBox Margin="10" Text="{Binding ValidateInputText,UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, ValidatesOnDataErrors=True, NotifyOnValidationError=True, ValidatesOnExceptions=True}"
Validation.ErrorTemplate="{StaticResource ErrorTemplate}">
    </TextBox>
    <Button Margin="10" Grid.Row="2"  Command="{Binding ValidateInputCommand}"/>

我的命令类是:

  public class RelayCommand : ICommand
{
    private Action WhatToExcute;
    private Func<bool> WhenToExecute;
    public RelayCommand(Action what,Func<bool>when )
    {
        WhatToExcute = what;
        WhenToExecute = when;
    }

    public event EventHandler CanExecuteChanged;

    public bool CanExecute(object parameter)
    {
       return WhenToExecute();
    }

    public void Execute(object parameter)
    {
         WhatToExcute();
    }
}

我的视图模型是:

    public class ViewModel : IDataErrorInfo , INotifyPropertyChanged
{

    public event PropertyChangedEventHandler PropertyChanged;

    Product product = new Product();

    public ViewModel()
    {
        ValidateInputCommand = new RelayCommand(action, valid);
    }
    public void action()
    {

    }
    public bool valid()
    {
        if (product.Name == null)
            return false;

        return true;
    }

    public string ValidateInputText
    {
        get { return product.Name; }
        set {
            product.Name = value;
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(ValidateInputText));
            }
        }
    }

    public RelayCommand ValidateInputCommand { get; set; }

    public string this[string columnName]
    {
        get
        {
            if ("ValidateInputText" == columnName)
            {

                if (String.IsNullOrEmpty(ValidateInputText))
                {
                    return "Please enter a Name";
                }
            }
            return "";
        }
    }

    public string Error
    {
        get
        {
            throw new NotImplementedException();
        }
    }
}

现在当我运行我的应用程序时,文本框的颜色为红色,当我输入时

字符变正常没问题,但是

按钮启用是假的,不会改变

所以我无法点击按钮。

我该怎么办?

【问题讨论】:

  • 您可以在 if ("ValidateInputText" == columnName) 子句的 else 中将其设置为 true
  • 在可能的情况下,您应该首选INotifyDataErrorInfo 作为您的视图模型。使用 WPF 更容易实现并且行为更一致。

标签: wpf mvvm idataerrorinfo


【解决方案1】:

RelayCommand 上的 CanExecuteChanged 事件永远不会被触发。因此,命令绑定到的按钮将永远不会重新评估其 IsEnabled 状态。您的视图模型可以通知命令其有效状态的变化,然后该命令应该引发其 CanExecuteChanged 事件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-31
    • 1970-01-01
    相关资源
    最近更新 更多