2011-06-28 9 views
13

私はカスタムバリデーターとテキストボックスをいくつか持っています:
( "間違った"データがオブジェクトに返されても(プロパティは文字列です)気にしません。結合は、検証のようなもののための右の場所ではありませんので、もしエラーが教えてくださいがある場合はボタンを押します。私はちょうど私が使用することができますValidation.ErrorTemplateのサポートなど)バリデーションエラーのボタンを無効にする

<ControlTemplate x:Key="validator" > 
    <DockPanel LastChildFill="True"> 
     <TextBlock DockPanel.Dock="Right" Foreground="Red" FontSize="12pt">!</TextBlock> 
     <Border BorderBrush="Red" BorderThickness="1.0"> 
      <AdornedElementPlaceholder /> 
     </Border> 
    </DockPanel> 
</ControlTemplate> 

<TextBox Height="23" Width="150" TextWrapping="Wrap" 
     Validation.ErrorTemplate="{StaticResource validator}"> 
     <TextBox.Text> 
      <Binding Path="StringProperty" UpdateSourceTrigger="LostFocus"> 
       <Binding.ValidationRules> 
        <local:NumbersOnly/> 
       </Binding.ValidationRules> 
      </Binding> 
     </TextBox.Text> 
</TextBox> 

どのように私は特定の無効にすることができますいずれかの検証エラーが発生した場合は、ボタンをクリックしますか?

<Button Content="DO Work" Height="57" HorizontalAlignment="Left" Name="button1" VerticalAlignment="Top" Width="234" Click="button1_Click" /> 
+0

可能な重複こちら:http://stackoverflow.com/questions/231052/using-wpf-validation-rules-and-disabling-a-save - ボタン – Damascus

+1

そこの投稿は質問に答えません... – anderi

答えて

34

を見てみましょう。 「txtName」という名前のTextBoxがあるとします。バリデーションエラーTextBoxのボタン "btnSave"を無効にする必要があります。ここで

は、あなたが何ができるかです:

<Button Content="Save" 
     Grid.Column="1" 
     Grid.Row="3" 
     HorizontalAlignment="Right" 
     Height="23" 
     Name="btnSave" 
     Width="75" 
     IsDefault="True" 
     Command="{Binding SaveProtocolCommand}" 
     Margin="3"> 
    <Button.Style> 
    <Style TargetType="Button"> 
     <Setter Property="IsEnabled" Value="False"/> 
     <Style.Triggers> 
     <MultiDataTrigger> 
      <MultiDataTrigger.Conditions> 
      <Condition Binding="{Binding Path=(Validation.HasError), ElementName=txtName}" Value="False"/> 
      </MultiDataTrigger.Conditions> 
      <Setter Property="IsEnabled" Value="True"/> 
     </MultiDataTrigger> 
     </Style.Triggers> 
    </Style> 
    </Button.Style> 
</Button> 

が、これはあなたを助けることを願っています。

+7

他の誰かがこれを知らない場合は、 'Validation.HasError'パスのまわりにカッコを入れなければなりません。 [これは添付プロパティを参照している方法です。](http://stackoverflow.com/a/14382796/1229237)それらがなければ、私はSystem.Windows.Data警告:40:BindingExpressionパスエラーを取得します。持っていました。 –

+0

@ S.Mishraもし私が2つのテキストボックスを持っていたら? 編集:NVM。ほぼ同じ行条件のバインドだけを追加するだけです。 –

+0

@p__d セクションにを追加することができます。各入力コントロールごとに –

0

MVVMを使用する場合は、インターフェイスICommandのCanExecuteメソッドを実装するだけです。 Button doesn't become disabled when command CanExecute is false

あなたが分離コードであなたのロジックを記述する場合は、ただボタンでIsEnabledのプロパティを使用します。

XAML:

<Button Name=_btn/> 

SomeForm.xaml.cs:

_btn.IsEnabled=false; 
3

CanExecuteをMVVMでは認証管理のために使用されますが、人々はそれを検証に使用します。 XAMLで行うのが最善の方法です。検証するフィールドが複数ある場合はコンバータが必要です(InverseAndBooleansToBooleanConverterは複数のブール値の実装です)。ここではそうする方法である:

XAMLコード(XAMLコードが表示されない場合、私は私がしようとした場合にも表示されていなかったのでごめんなさい):

<Button Name="Button_Test" Content="Test"> 
    <Button.IsEnabled> 
     <MultiBinding Converter="{StaticResource InverseAndBooleansToBooleanConverter}" Mode="TwoWay"> 
      <Binding ElementName="TextBox_Field1" Path="(Validation.HasError)" /> 
      <Binding ElementName="TextBox_Field2" Path="(Validation.HasError)" /> 
      <Binding ElementName="TextBox_Field3" Path="(Validation.HasError)" /> 
     </MultiBinding> 
    </Button.IsEnabled> 
</Button> 

コンバータ

public class InverseAndBooleansToBooleanConverter : IMultiValueConverter 
{ 
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     if (values.LongLength > 0) 
     { 
      foreach (var value in values) 
      { 
       if (value is bool && (bool)value) 
       { 
        return false; 
       } 
      } 
     }  
     return true; 
    } 

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) 
    { 
      throw new NotImplementedException(); 
    } 
} 
1

あなたのTextBlockにこれを追加します。

Validation.Error="Save_Error" 

分離コード(xaml.cs):

public partial class MyView : Window 
{ 
    private int _noOfErrorsOnScreen = 0; 

    public MyView() 
    { 
     InitializeComponent(); 
    } 


    private void Save_Error(object sender, ValidationErrorEventArgs e) 
    { 
     if (e.Action == ValidationErrorEventAction.Added) 
      _noOfErrorsOnScreen++; 
     else 
      _noOfErrorsOnScreen--; 

     Save.IsEnabled = _noOfErrorsOnScreen > 0 ? false : true; 

    } 
} 
関連する問題