What's a clean way of removing order-dependency and redundancy when event chaining?
Say you have a dialog with 3 fields.
- Case 1. Entering field 1 will default field 2 and field 3 (eventHandler1)
- Case 2. Entering field 2 will default field 3 (eventHandler2)
- Case 3. Entering field 3 doesn't default anything.
There are two problems with this:
Redundancy: w/o additional effort, eventHandler1 implicitly triggers eventHandler2. In this toy example, it's not a problem. But expand the scenario out to a larger number of fields and w/o care, the overhead can become huge.
Order-dependency: I think field 3 will default with eventHandler2. But in either case; sometimes, eventHandler1's default may be correct.开发者_Python百科 Other-times, eventHandler2's default may be correct.
Is there a clean, structured way of handling this in C# w/o having to deal with a massive number of states?
Ok, what you will need to do is seperate your layers in the winforms app. Design a class that does the work of determining which fields impact which other fields. Each of the properties in this class will use custom code in the property setter to modify dependant properties, which will in turn send a signal to the window that their contents have changed.
The "View" (View-like) class will be a container for your first class, and handle input events, calling methods from the second class to handle the results. Lastly it will update the other fields when commanded to do so from the other class.
Old, incorrect answer here (Didn't catch the Winforms tag, silly me
The simplest answer is good View-Model and View seperation, but without a code sample, it's hard to determine if MVVM is appropriate.
So your View xaml will have definitions for three (four, ten, whatever) fields, each databound to a property in your ViewModel.
The setter for each property will handle the logic of setting dependant values in other properties. As each property is set, they should notify when changed, and the UI will update without any further work from you.
Much less coupled; much more structured.
精彩评论