Validation Application Analysis of Data Binding in WPF

Source: Internet
Author: User

I was writingWPF bugList SeriesBut write it to the third article (suddenly disappearedListviewitem).BugThe logic is very complex. In addition, it requires a lot of basic knowledge. If you do not explain it clearly first, you are afraid that someone will use "no ".ValiationNo. "Wait for a reply to hit the scene. Pai_^

 

From the perspective of design and applicationDatabindingOfValidationAnalysis, assuming that the reader has usedDatabindingInValidation.Important IntroductionWPFMediumDatabindingOfValidation. For more information about data binding, seeMsdn.

 

Some people are not happy to use data binding., Whether it isWPF or winform. I think it is better to control data synchronization and update? If it's just a login box or somethingProgramAnd there is absolutely no problem. However, in enterprise-level programs with a large number of hierarchical data structuresUIAnd background data updates, not only heavy workload, but also prone to errors. (WPFAlthough there are manyBugBut we shouldn't use it .)

 

In most casesValiationruleSimple inheritanceRuleYou can complete the verification of most data bindings. For exampleStringrequiredrule,Lengthcontraintrule,RegexvalidationruleWaitCodeplexSuchSimple ruleLibrary Project. (Surprisingly, there are only a few lines.CodeThe things are stillBeta)

 

But for a little more complexValidation, AverageValidationruleYou can't do anything about it.

 

For examplePersonClass.

 

Public Class Person

{

Public IntMaxage = 1000;

Public IntMinage = 0;

Public IntAge {Get;Set;}

}

 

In this case, verifyAgeTo obtainMaxageAndMinage. However, this value. NET Framework 3.0OfValidationruleCannot be obtained (3.5 SP1You canBindinggroupOfValidation). This is only found in the sibling attribute, ifPersonThere isParentAttribute, we want to verifyParentIf it exists, you must obtain allPerson.

 

Then we will sacrifice. NET Framework 3.5IntroducedDataerrorvalidationruleOr. NET Framework 3.5 SP1IntroducedBindinggroup. (Unreleased. NET Framework 4.0ActuallyWPFIs added with multiple important components .)

 

PS: Face3.0,3.5Simple, do not want to follow up frequently. NetAnd cannot be updated. (Or the new version is more attractive)

 

For the sake of simplicity, this article will focus onDataerrorvalidationruleAfter all,IdataerrorinfoInterface as early. NET Framework 1.0. If the data definition in the legacy system has used this interface for data verificationDataerrorvalidationrulePortWPFIt will also bring some convenience. (If you really want to move it .)

 

UseIdataerrorinfoInterface, simple, you can directly implement this interface, but it will make this class Very bloated, because it will write a pile of data verification logic in the data class. We know that the functions of classes should be single, so we need to establish a complete set of classes for data verification. Separate the verification logic. This is a design issue unrelated to the topic and will not be discussed here. When using data verification, we have to face the following facts.

1.In some cases, a Data Change (such as deleting a data item) may result in multiple verification errors. Therefore, you need to maintainErrorsList. Then, a seriesCrudThe operation follows. This is also okay. The main problems are as follows.

2.The data verification logic is for the data class, that is, each instance of this class should have the same or similar verification logic. However,Data BindingOfValidationIt is for data instances. There is no problem with data instances. But it may be to save memory,WPFThe implementationValidationInconvenience --UIInvisible controls are not automatically implemented.Validation.

3. because the fact 2 exists, we will manually validate the data . The problem persists. Manually Validation , UI I don't know the layer. If an error occurs, UI does not comply with errortemplate automatic change UI (for example, automatically display the red box ). How can I let UI know that it has an error? One method is to trigger the propertychanged event.

4.PartBusinessobjectWithIsdirtyProperties (seeCsla. netFrameworkIs used to identify whether the object has changed. SometimesPropertychangedEventIsdirtyAttribute. And this corresponds3Solution conflict ...... A series of problems have emerged.

 

For the above question, I would like to writeWPF bugListArticle. But this is notWPFOfBugIs inconvenient to use ). So I wrote an explanation separately. Hope to be available or to be usedWPFMediumValidationSome tips.

 

Due to the time limit, a lot of content has not been expanded, but it is just a point. If something is too ugly to write-not polite.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.