2011-01-01 16 views
5

こんにちは 私のアプリケーションでいくつかのテキストボックスを検証する必要があります。私は検証ルール "DataErrorValidationRule"を使用することに決めました。だから私のクラスで私はIDataErrorInfoインターフェイスを実装し、適切な関数を書いています。私のXAMLコードでは、私はテキストボックスにバインドし、検証ルールを追加し、このテキストボックスのwpf - validation - ツールチップを表示して「実行」ボタンを無効にする方法

<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> 

検証はOKです - 私は、データが間違っている場合は、赤枠がテキストボックスに表示されますを意味します。しかし、私がする必要があるのは、そのテキストボックスにツールチップを表示することですが、テキストボックスに間違ったデータがある場合は、「実行」ボタンを無効にする必要があります。タトゥーをする最良の方法は何ですか?

EDIT 最初の問題は解決しましたが、別の問題があります。ボタンを検証するためにMultiBindingsを使用する必要があります。だから私はその

<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> 

ようSTHをした私は、このコンバータでInvalidCastExceptionが取得しかし私のコンバータは、その

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 
} 

のように見えます。その場合、適切なキャストは何ですか? HasErrorがbool型のように私はthoghtので、私はboolにキャストする必要があります。

+0

「タイプ」とはどういう意味ですか?それを "if(values.Cast ().Any(val => val))"に置き換えます。 " – vorrtex

答えて

2

検証要約を作成する場合は、[実行]ボタンのIsEnabledプロパティをHasErrorsプロパティにバインドできます。

HasErrorsがfalseの場合(またはその逆の場合)、IsEnabledtrueにしたい場合は、中間プロパティまたはコンバーターを使用する必要があります。

+1

検証要約はどのように作成できますか? – stylus

+0

@validator - ここに1つあります - http://codeblitz.wordpress.com/2009/05/12/wpf-validation-summary-control/ - 私はSilverlightと混乱していました、ごめんなさい。 – ChrisF

6
<Window.Resources> 
    <Style x:Key="ElementInError" TargetType="{x:Type FrameworkElement}"> 
     <Style.Triggers> 
      <Trigger Property="Validation.HasError" Value="True"> 
       <Setter Property="ToolTip" 
        Value="{Binding (Validation.Errors)[0].ErrorContent, RelativeSource={x:Static RelativeSource.Self}}"/> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 
</Window.Resources> 
<!-- ... --> 
<TextBox x:Name="txtName" Style="{StaticResource ElementInError}"> 
    <!-- ... -->   
</TextBox> 
<!-- ... --> 
     <Button x:Name="OkButton" Content="Ok" Margin="5" Click="OkButton_Click"> 
      <Button.Style> 
       <Style TargetType="Button"> 
        <Style.Triggers> 
         <DataTrigger Binding="{Binding ElementName=txtName,Path=(Validation.HasError)}" Value="True"> 
          <Setter Property="IsEnabled" Value="False" /> 
         </DataTrigger> 
        </Style.Triggers> 
       </Style> 
      </Button.Style> 
     </Button> 
+0

答えをありがとう、私の質問でもう一度見てみることができます - 私はそれを編集しました – stylus

17

あなた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からの例)

<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を実装してcommおよび結合。ここで

EDIT

は完全実施例です。 2つのテキストボックスがあるウィンドウが表示されます。このボタンは、両方のテキストボックスが空でない場合にのみ有効になります。 ValidationDemoと呼ばれるプロジェクトを作成し、その中に以下のファイルを置く:

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

モデル。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)); 
     } 
    } 

    } 

} 
+0

Unfortunatellyそれは動作していません。私のテキストボックスの妥当性検査が失敗したという事実にもかかわらず、Okボタンはまだ有効です。 – stylus

+0

@validator:私は実際の例を追加しました。 – TomBot

関連する問題