Which design pattern should be used in C # with WPF to dynamically "change object classes" through the user interface?

I'm currently working on a C # WPF application that allows you to create a graph (that is, a bunch of vertices connected through edges), and then use this graph as a template to find it in a bunch of other (larger) graphs (" host "). Each element of the graph has at least a type and a label.

Graph elements (edges and vertices) can have different "types of constraints".

For example, there may be restrictions at the top. The label for this vertex must be "Vertex A" or "This vertex type" must be in the set {type A, Type B, Type H} ".

For edges, the types of constraints are a bit more complicated. The edge can be limited to either a β€œsimple” edge or a β€œpath edge”. The edge of the path between two vertices in the template graph can be considered a placeholder that allows you to find multiple edges (and vertices) between two vertices in the host graph. In contrast, a simple edge allows you to find only one edge (and without additional vertices) in the host graph.

If an edge has a path constraint (instead of the usual edge constraint), it has some additional properties, such as the minimum path length or valid vertex types allowed on the path.

The type constraint structure can be seen in this UML class diagram: image1

~~~

Now from the point of view of the user interface: the user should be able to customize if the edge has a path restriction. If so, the necessary additional controls (TextBoxes, ListBoxes, etc.) should be automatically displayed for additional parameters. Changes in all controls should automatically be reflected in the data structure.

Here the user interface should behave as if changing the settings of the selected edge: image2 (There should also be a scroll bar on the right side on the right side, which allows you to scroll down and configure also acceptable types of edges on the path. Also ignore the settings for overlapping vertices and borders at the moment.)

~~~

Finally, my questions boil down to the following:

How do you implement such a dynamic object class change while maintaining the style of WPF data binding? (With a change in the class of dynamic objects, I mean that by clicking the "Treat this edge as a path" checkbox, the selected edge will receive a different type of constraint.)

Do I really need to create an old-school event listener that fires when the "Treat this edge as a path" checkbox changes and use it to manually update the visibility of other sidebar controls?

Would it help in any way if I somehow change the structure of the restriction class?

+4
source share
2 answers

The sample design you're behind is probably MVVM . It looks like you are currently connecting your user interface to your model directly, without viewing the model between them. Sometimes you can avoid it, sometimes not.

You can write a value converter applied to the binding of the IsChecked property, which converts the boolean value into the correct instance of the restriction class and vice versa. As the state of the checkbox changes, another instance is created and assigned to your model. This will be reflected in the user interface with a choice of data template.

Most likely, it will be more difficult, for example, if you want to save values ​​between state changes. This is where you would like to introduce a view model to sit between the user interface and the model. The view model closely reflects the user interface and has the corresponding logical property, and with this change, you can change the model accordingly.

In any case, there should be no reason to bind to events on the controls: try to handle all this in view models.

+3
source

Well, I'm really not talking about design patterns, so I am not responding to these conditions. I'm really talking about WPF, and template design usually turns out to be what I already did, but didn't know what it was called.

WPF doesn't care which type is nothing. If you display it using a DataTemplate in a ContentControl any type, you may have one for each type on which the object may really need special treatment, and WPF will take care of this for you. A DataTemplateSelector might be a good idea for more complex logic for selecting user interface elements for a given type, which, in my experience, tends to occur in many supposedly simple scenarios that, as it turns out, won't. The disadvantage is that the DataTemplateSelector needs to know about all your data types and templates in order to be able to choose between them.

This really does work for the sidebar, although the rendering of the actual graph probably needs to be done in a more holistic way and, I would say, a completely different problem.

+3
source

Source: https://habr.com/ru/post/1483147/


All Articles