Dynamically create and style WPF controls from XML

I want to complete a series of steps that include a complete test. Some of these steps are automatic (therefore informational), while others require user interaction. During compilation, the testing steps are unknown; they use MEF to load.

I currently have something like

public abstract class TestRunnerBase { public abstract void Run(); } 

With a list of such steps:

  List<TestRunnerBase> Steps = new List<TestRunnerBase>(); 

Thus, all the data representing the test serializable, and which is working fine so far. However, I really need the user to load the test from XML, then look at the parameters, displaying the information on the screen and collecting the results.

But trying to figure out how to create a control for data that was not known at compile time ended up being a little stuck on the best approach.

I'm going to do this, I will have a list of user controls (1 step), and the GUI will display the first step, waiting for the completion of this control (did I think here that the raised event can work?), And then display the following if available, and so on until the test is completed.

So can this be done in WPF? Can you create a stack of controls in WPF that can raise the same event to the parent container, or is there a better way to do this?

But if I also use an abstract class, I cannot then derive a control from it also, as a rule, without multiple inheritance in C #.

+4
source share
2 answers

I would use MVVM and create a viewmodel that understands how to navigate the list of steps, providing a wizard type structure (prev / next) and displaying the current step.

I assume that although you have different types of potentially unknown steps, you have a specific set of input parameters (bool, text, date, int, etc.), you can use an abstract property on your test platform that identifies entry is required (or not) using an enumeration, which must be redefined.

Then you can use datatemplates and / or data triggers to control what is shown for each step of the test. The main viewing model can verify that the conditions are suitable for moving on to the next step (perhaps checking for your test).

Some psuedo code so you might think:

 public enum TestInput { None, Bool, Text } public abstract class TestRunnerBase { public abstract TestInput TestInput { get; } public bool BoolInput { get; set; } public string TextInput { get; set; } public abstract bool CanRun() public abstract void Run(); } public class MainViewModel { List<TestRunnerBase> Steps = new List<TestRunnerBase>(); public TestRunnerBase CurrentStep {get;set;}; public MainViewModel() { //loads the Steps CurrentStep = Steps } public Command RunStepCommand { if (CurrentStep.CanRun()) { CurrentStep.Run(); CurrentStep = Steps.Next(); //you get the idea } } } 

For your XAML, you must bind ContentPresenter to CurrentStep and use a datatemplate (and possibly data triggers) to control what is visible to the user (and, of course, related to the user interface).

+2
source

In WPF, you can dynamically create controls using XAML. Just create the XAML fragment with the desired layout (programmatically or manually) and use XamlReader.Parse to create the entire tree from the fragment. The returned object can then be inserted somewhere in the visual tree of your window.

To generate events from the visual tree generated by XamlReader, you can use routed events .

+1
source

All Articles