首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WPF MVVM IDataErrorInfo

WPF MVVM IDataErrorInfo
EN

Stack Overflow用户
提问于 2016-03-30 17:41:45
回答 1查看 1.2K关注 0票数 0

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

我添加了一个验证,如果文本框是空的,文本框的颜色会更改为红色;

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

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

我的XAML代码是:

代码语言:javascript
复制
    <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}"/>

我的指挥课是:

代码语言:javascript
复制
  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();
    }
}

我的看法是:

代码语言:javascript
复制
    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();
        }
    }
}

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

字符它更改为正常,这是可以的,但

按钮启用为false,且不会更改。

所以我不能点击按钮。

我该怎么办?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-30 19:01:56

CanExecuteChanged事件在您的RelayCommand上永远不会被解雇。因此,命令绑定到的按钮将永远不会重新评估其IsEnabled状态。您的视图模型可以将其有效状态的更改通知该命令,并且该命令将引发它的CanExecuteChanged事件。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36315958

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档