3

How to disable a WPF button based on the ValidationRule class? (The code below works fine to highlight TextBox)

<Button x:Uid="btnSave" Content="{lex:LocTextExtension Key=Save, Dict=Resources, Assembly=PreShow.Player}"   Height="23" HorizontalAlignment="Center"  Name="btnSave" VerticalAlignment="Top" Width="75" IsDefault="True">

XAML:

<Window.Resources>
        <k:PlayerConfiguration x:Key="ods"/>
        <ControlTemplate x:Key="validationTemplate">
            <DockPanel>
                <TextBlock Foreground="Red" FontSize="20">!</TextBlock>
                <AdornedElementPlaceholder/>
            </DockPanel>
        </ControlTemplate>
        <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>
    </Window.Resources>




<TextBox Grid.Column="1" Height="23" HorizontalAlignment="Left" Name="textBox1"     VerticalAlignment="Top" Width="277">
   <TextBox.Text>
     <Binding Path="UserName" Source="{StaticResource ods}"  UpdateSourceTrigger="PropertyChanged" >
       <Binding.ValidationRules>
           <c:ConfigValidationRule />
             </Binding.ValidationRules>
    </Binding>
   </TextBox.Text>
</TextBox>

C#

 public class ConfigValidationRule : ValidationRule
    {
        public ConfigValidationRule()
        {
            HasError = true;
        }

        public bool HasError { set; get; }

        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            try
            {
                if (((string)value).Length > 0)
                {
                    HasError = false;
                    return new ValidationResult(true, null);
                }
                else
                {
                    HasError = true;
                    return new ValidationResult(false, "!");
                }
            }
            catch
            {
                HasError = true;
                return new ValidationResult(false, "!");
            }
        }
    }

There are plenty of examples but all of them doesn't have a SAVE BUTTON.

  1. http://www.codeproject.com/Articles/15239/Validation-in-Windows-Presentation-Foundation
  2. http://go.microsoft.com/fwlink/?LinkID=159972
NoWar
  • 36,338
  • 80
  • 323
  • 498
  • 2
    Possible duplicate: http://stackoverflow.com/questions/757590/disable-save-button-in-wpf-if-validation-fails – tenorsax Jan 23 '12 at 18:20
  • http://stackoverflow.com/questions/231052/using-wpf-validation-rules-and-disabling-a-save-button – tenorsax Jan 23 '12 at 18:22
  • 1
    Guys it is not the same as you suggested to take a look. My solution has a ValidationRule class. So I want to play around it if it's possible. IE http://www.switchonthecode.com/tutorials/wpf-tutorial-binding-validation-rules or http://wpftutorial.net/DataValidation.html – NoWar Jan 23 '12 at 18:25
  • How many buttons are you using.. have you looked also at BindingGroup ..? http://msdn.microsoft.com/en-us/library/system.componentmodel.idataerrorinfo.aspx there is a complete sample and detail explaination: http://codeblitz.wordpress.com/2009/05/08/wpf-validation-made-easy-with-idataerrorinfo/ http://karlshifflett.wordpress.com/mvvm/input-validation-ui-exceptions-model-validation-errors/ – MethodMan Jan 23 '12 at 18:26
  • @DJKRAZE I used it http://go.microsoft.com/fwlink/?LinkID=159972 as well. But It doesn't have any sample to disable a Save button... :( – NoWar Jan 23 '12 at 18:32
  • bind the validatoin.HasError to the buttons isEnabled property and use a converter, or you could use commanding and then you have a possibility to use a boolean property to determine if the command can be executed or not if not then the button will be disabled automatically. – BigL Jan 23 '12 at 19:08
  • @BigL "bind the validatoin.HasError to the buttons isEnabled property" -- pls provide any code... – NoWar Jan 23 '12 at 19:12
  • IsEnabled="{Binding ElementName=textBox1,Path=Validation.HasError}" Hopefully this will work i would have to test it but i let you do that. :) – BigL Jan 23 '12 at 19:31
  • 2
    Personally I would implement `IDataErrorInfo` in your `ViewModel` and make the `SaveCommand.CanExecute()` only return true if `ViewModel.IsValid`. Then it will automatically take care of disabling the button if the SaveCommand is not supposed to execute. – Rachel Jan 23 '12 at 19:31
  • @Rachel yes that's true i would do the same and suggested something like that, only that i think he would have to rewrite and design his solution to MVVM model. :) – BigL Jan 23 '12 at 19:33

0 Answers0