wpf - 验证 - 如何显示工具提示和禁用“运行";按钮 [英] wpf - validation - how to show tooltips and disable "run" button

查看:25
本文介绍了wpf - 验证 - 如何显示工具提示和禁用“运行";按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨我需要验证我的应用程序中的一些文本框.我决定使用验证规则数据错误验证规则".这就是为什么在我的课程中我实现了 IDataErrorInfo 接口并编写了适当的函数.在我的 xaml 代码中,我向文本框添加了绑定和验证规则

Hi I need to validate some of textboxes in my application. I decied to use validation rule "DataErrorValidationRule". That's why in my class I implemented IDataErrorInfo interface and wrote aproperiate functions. In my xaml code I added bindings and validation rules to textboxes

 <TextBox x:Name="txtName" Grid.Column="3" Grid.Row="1"  TextAlignment="Center" >
                        <TextBox.Text>
                            <Binding Path="Name" >
                                <Binding.ValidationRules>
                                    <DataErrorValidationRule></DataErrorValidationRule>
                                </Binding.ValidationRules>
                            </Binding>
                        </TextBox.Text>
                    </TextBox>

验证此文本框没问题 - 我的意思是如果数据错误,文本框上会出现红框.但是,我需要做的是在该文本框上显示工具提示,但更重要的是,如果任何文本框有错误数据,我必须禁用运行"按钮.最好的方法是什么??

Validation of this textbox is OK - I mean red frame appears on textbox if data is wrong. However what I need to do is to show tooltip on that textbox, but what is more important I have to disable button "Run" if any textboxes have wrong data. What is the best way to do taht ??

编辑第一个问题解决了,但我还有一个.我需要使用 MultiBindings 来验证我的按钮.所以我就这样做了

EDIT First problem was solved, but I have an another. I need to use MultiBindings to validate my Button. So I did sth like that

 <Button x:Name="btnArrange"  Grid.Column="0"  Content="Rozmieść" Click="btnArrange_Click" >
                <Button.Style>
                    <Style TargetType="Button">
                        <Style.Triggers>
                            <DataTrigger Value="False">
                                <DataTrigger.Binding>
                                    <MultiBinding Converter="{StaticResource BindingConverter}">
                                        <Binding ElementName="txtName" Path="Validation.HasError" />
                                        <Binding ElementName="txtSurname" Path="Validation.HasError"/>
                                        <Binding ElementName="txtAddress" Path="Validation.HasError"/>

                                    </MultiBinding>
                                </DataTrigger.Binding>
                                <Setter Property="IsEnabled" Value="False"/>

                            </DataTrigger>
                        </Style.Triggers>
                    </Style>        
                </Button.Style>

        </Button>

我的转换器看起来像那样

My Converter looks like that

 public  class Converters : IMultiValueConverter
{

    #region IMultiValueConverter Members

    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if(values !=null && values.Length > 0)
        {


            if (values.Cast<type>().Count(val => val) > 0)
                return false;
            return true;
        }
        return false;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        return null;
    }

    #endregion
}

但是我在这个转换器中得到了 invalidCastException.在这种情况下,什么是正确的演员表?我认为 HasError 是一个 bool 类型,所以我应该转换为 bool.

However I get invalidCastException in this converter. What is a proper cast in that case? I thoght as if HasError is a bool type so I should cast to bool.

推荐答案

要在工具提示中显示错误消息,请将其放入您的 Application.Resources:

To show the error message in a tool tip put this into your Application.Resources:

<Style x:Key="textBoxInError" TargetType="{x:Type 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>

(来自 http://msdn 的示例.microsoft.com/en-us/library/system.windows.controls.validation.errortemplate.aspx )

要启用/禁用按钮,您可以使用以下内容

To enable/disable a button you could use something along the line of

<Button x:Name="btnOK" Content="OK" IsDefault="True" Click="btnOK_Click">
  <Button.Style>
    <Style TargetType="{x:Type Button}">
      <Setter Property="IsEnabled" Value="false" />
      <Style.Triggers>
        <MultiDataTrigger>
          <MultiDataTrigger.Conditions>
            <Condition Binding="{Binding ElementName=txt1, Path=(Validation.HasError)}" Value="false" />
            <Condition Binding="{Binding ElementName=txt2, Path=(Validation.HasError)}" Value="false" />
          </MultiDataTrigger.Conditions>
          <Setter Property="IsEnabled" Value="true" />
        </MultiDataTrigger>
      </Style.Triggers>
    </Style>
  </Button.Style>
</Button>

或者你可以实现 ICommand 并使用命令绑定.

or you could implement ICommand and use command binding.

编辑

这是一个完整的示例.它显示一个带有两个文本框的窗口.当且仅当两个 TextBox 都不为空时,才会启用 Button.创建一个名为 ValidationDemo 的项目并将以下文件放入其中:

Here is a fully working example. It displays a window with two TextBoxes. The Button is enabled if and only if both TextBoxes are non-empty. Create a project called ValidationDemo and put the following files in it:

MainWindow.xaml:

<Window x:Class="ValidationDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="146" Width="223">
  <Window.Resources>
    <Style TargetType="{x:Type 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>
    <Label Content="A" Height="28" HorizontalAlignment="Left" Margin="46,7,0,0" Name="label1" VerticalAlignment="Top" />
    <TextBox Name="txtA" Text="{Binding Path=TextA, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" Height="23" HorizontalAlignment="Left" Margin="69,12,0,0" VerticalAlignment="Top" Width="120" />
    <Label Content="B" Height="28" HorizontalAlignment="Left" Margin="46,39,0,0" Name="label2" VerticalAlignment="Top" />
    <TextBox Name="txtB" Text="{Binding Path=TextB, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" Height="23" HorizontalAlignment="Left" Margin="69,41,0,0" VerticalAlignment="Top" Width="120" />
    <Button Name="btnOk" Content="OK" Height="23" HorizontalAlignment="Left" Margin="114,70,0,0" VerticalAlignment="Top" Width="75" Click="btnOk_Click">
      <Button.Style>
        <Style TargetType="{x:Type Button}">
          <Setter Property="IsEnabled" Value="false" />
          <Style.Triggers>
            <MultiDataTrigger>
              <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding ElementName=txtA, Path=(Validation.HasError)}" Value="false" />
                <Condition Binding="{Binding ElementName=txtB, Path=(Validation.HasError)}" Value="false" />
              </MultiDataTrigger.Conditions>
              <Setter Property="IsEnabled" Value="true" />
            </MultiDataTrigger>
          </Style.Triggers>
        </Style>
      </Button.Style>
    </Button>
  </Grid>
</Window>

MainWindow.xaml.cs:

using System.Windows;

namespace ValidationDemo
{
  /// <summary>
  /// Interaction logic for MainWindow.xaml
  /// </summary>
  public partial class MainWindow : Window
  {

    private Model model = new Model();

    public MainWindow()
    {
      InitializeComponent();
      this.DataContext = this.model;
    }

    private void btnOk_Click(object sender, RoutedEventArgs e)
    {
      Application.Current.Shutdown();
    }
  }
}

Model.cs:

using System;
using System.ComponentModel;

namespace ValidationDemo
{
  public class Model : INotifyPropertyChanged, IDataErrorInfo
  {
    public event PropertyChangedEventHandler PropertyChanged;

    private string textA = string.Empty;
    public string TextA
    {
      get
      {
        return this.textA;
      }
      set
      {
        if (this.textA != value)
        {
          this.textA = value;
          this.OnPropertyChanged("TextA");
        }
      }
    }

    private string textB = string.Empty;
    public string TextB
    {
      get
      {
        return this.textB;
      }
      set
      {
        if (this.textB != value)
        {
          this.textB = value;
          this.OnPropertyChanged("TextB");
        }
      }
    }

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

    public string this[string columnName]
    {
      get
      {
        string result = string.Empty;
        switch (columnName)
        {
          case "TextA":
            if (string.IsNullOrEmpty(this.textA))
            {
              result = "'A' must not be empty";
            }
            break;
          case "TextB":
            if (string.IsNullOrEmpty(this.textA))
            {
              result = "'B' must not be empty";
            }
            break;
        }
        return result;
      }
    }

    protected virtual void OnPropertyChanged(string propertyName)
    {
      if (this.PropertyChanged != null)
      {
        this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
      }
    }

  }

}

这篇关于wpf - 验证 - 如何显示工具提示和禁用“运行";按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆