Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 174339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:30:15+00:00 2026-05-11T13:30:15+00:00

I’ve adopted what appears to be the standard way of validating textboxes in WPF

  • 0

I’ve adopted what appears to be the standard way of validating textboxes in WPF using the IDataErrorInfo interface and styles as shown below. However, how can I disable the Save button when the page becomes invalid? Is this done somehow through triggers?

Default Public ReadOnly Property Item(ByVal propertyName As String) As String Implements IDataErrorInfo.Item     Get         Dim valid As Boolean = True         If propertyName = 'IncidentCategory' Then             valid = True             If Len(IncidentCategory) = 0 Then                 valid = False             End If             If Not valid Then                 Return 'Incident category is required'             End If         End If          Return Nothing      End Get End Property 

<Style TargetType='{x:Type TextBox}'>     <Setter Property='Margin' Value='3' />     <Setter Property='Height' Value='23' />     <Setter Property='HorizontalAlignment' Value='Left' />     <Setter Property='Validation.ErrorTemplate'>         <Setter.Value>             <ControlTemplate>                 <DockPanel LastChildFill='True'>                     <Border BorderBrush='Red' BorderThickness='1'>                         <AdornedElementPlaceholder Name='MyAdorner' />                     </Border>                 </DockPanel>             </ControlTemplate>         </Setter.Value>     </Setter>     <Style.Triggers>         <Trigger Property='Validation.HasError' Value='true'>             <Setter Property='ToolTip'  Value='{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}' />         </Trigger>     </Style.Triggers> </Style> 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T13:30:16+00:00Added an answer on May 11, 2026 at 1:30 pm

    A couple of things:

    First, I would recommend using the RoutedCommand ApplicationCommands.Save for implementing the handling of the save button.

    If you haven’t checked out the WPF Command model, you can get the scoop here.

    <Button Content='Save' Command='Save'> 

    Now, to implement the functionality, you can add a command binding to the Window/UserControl or to the Button itself:

        <Button.CommandBindings>         <CommandBinding Command='Save'                          Executed='Save_Executed' CanExecute='Save_CanExecute'/>     </Button.CommandBindings> </Button> 

    Implement these in code behind:

    private void Save_Executed(object sender, ExecutedRoutedEventArgs e) { }  private void Save_CanExecute(object sender, CanExecuteRoutedEventArgs e) { } 

    In Save_CanExecute, set e.CanExecute based on the validity of the binding on the text box.

    If you want to implement using the MVVM (Model-View-ViewModel) design pattern, check out Josh Smith’s post on CommandSinkBinding.

    One final note: If you want the enable/disable to be updated as soon as the value in the TextBox is changed, set UpdateSourceTrigger='PropertyChanged' on the binding for the TextBox.

    EDIT: If you want to validate/invalidate based on all of the bindings in the control, here are a few suggestions.

    1) You are already implementing IDataErrorInfo. Try implementing the IDataErrorInfo.Error property such that it returns the string that is invalid for all of the properties that you are binding to. This will only work if your whole control is binding to a single data object. Set e.CanExecute = string.IsNullOrEmpty(data.Error);

    2) Use reflection to get all of the public static DependencyProperties on the relevant controls. Then call BindingOperations.GetBindingExpression(relevantControl, DependencyProperty) in a loop on each property so you can test the validation.

    3) In the constructor, manually create a collection of all bound properties on nested controls. In CanExecute, iterate through this collection and validate each DependencyObject/DepencyProperty combination by using BindingOperation.GetBindingExpression() to get expressions and then examining BindingExpression.HasError.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
Does anyone know how can I replace this 2 symbol below from the string
I have thousands of HTML files to process using Groovy/Java and I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.