【问题标题】:Disable validation for a button in IDataErrorInfo禁用 IDataErrorInfo 中按钮的验证
【发布时间】:2015-02-08 17:07:46
【问题描述】:

我的视图上有两个按钮“搜索”和“清除”,我的视图模型上有两个命令。我已经在我的 ViewModel 上实现了 IDataErrorInfo 并验证了输入字段。如何禁用清除按钮的验证?

<TextBox Text="{Binding SearchText, Mode=TwoWay, ValidatesOnDataErrors=True, NotifyOnValidationError=True, ValidatesOnExceptions=True}"
Validation.ErrorTemplate="{StaticResource ErrorTemplate}"/>
<Button Content="Search" Command="{Binding SearchCommand}" />
<Button Content="Clear" Command="{Binding ClearCommand}" />

【问题讨论】:

  • 您的要求不是更清楚。您是否要根据搜索文本框中的文本禁用清除按钮。示例 如果 seach 不为 null 或为空,则启用清除按钮。请解释

标签: wpf mvvm idataerrorinfo


【解决方案1】:

我假设您想根据搜索文本框中的验证启用/禁用清除按钮。我已经使用 MvvmLight Relaycommand 使用 GalaSoft.MvvmLight.CommandWpf 使用命名空间从最新的 MVVMLight 进行命令;参考下面的代码。

<Window x:Class="DataTemplateSelector_Learning.Window3"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window3" Height="300" Width="300">    
<Grid>
    <StackPanel>
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="Search Text"/>
            <TextBox Width="100" Text="{Binding SearchText, UpdateSourceTrigger=PropertyChanged,Mode=TwoWay,
                ValidatesOnDataErrors=True, NotifyOnValidationError=True, ValidatesOnExceptions=True}"  />
        </StackPanel>
        <Button Content="Search" Command="{Binding SearchCommand}" />
        <Button Content="Clear" Command="{Binding ClearCommand}" />
    </StackPanel>
</Grid>

public partial class Window3 : Window
{
    public Window3()
    {
        InitializeComponent();
        this.DataContext = new ViewModel();
    }
}
class ViewModel:INotifyPropertyChanged,IDataErrorInfo
{
    private string searchText;
    private bool enableClear;

    public string SearchText
    {
        get { return searchText; }
        set { searchText = value; Notify("SearchText"); }
    }

    public ICommand SearchCommand { get; set; }
    public ICommand ClearCommand { get; set; }

    public ViewModel()
    {
        ClearCommand = new RelayCommand(OnClear, CanClear);
    }

    private bool CanClear()
    {
        return enableClear;
    }

    private void OnClear()
    {

    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void Notify(string propName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propName)); 
        }               

    }

    public string Error
    {
        get { return String.Empty; }
    }

    public string this[string columnName]
    {
        get
        {
            String errorMessage = String.Empty;
            if (!string.IsNullOrEmpty(SearchText))
            {
                if (SearchText.Length > 0)
                {
                    enableClear = true;
                }
                else
                {
                    enableClear = false;
                }
            }

            return errorMessage;
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-02
    • 2021-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-15
    • 1970-01-01
    相关资源
    最近更新 更多