Search code examples
wpfmvvmidataerrorinfo

WPF MVVM IDataErrorInfo


i have an app in MVVM pattern ; it contains one textbox and one button;

i add a validation that if textbox is empty , textbox color change to red;

and at this point i want that the button enable be false till the user

enter character in textbox and then button enable change to true;

my XAML Code is:

    <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}"/>

and my command class is:

  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();
    }
}

and my viewmodel is:

    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();
        }
    }
}

now when i run my app , the color of text box is red and when i enter a

character it change to normal that is ok but the

button enable is false and does not change

so i could not click button.

what should i do?


Solution

  • The CanExecuteChanged event on your RelayCommand never gets fired. Therefore the button to which the command has been bound will never re-evaluate its IsEnabled status. Your view model could notify the command on changes in its valid status and the command should in turn raise its CanExecuteChanged event.