ConceptHMI Web Help
Creating views to interact with the business model
How to define a custom view for a component

If the default view doesn't suit you, a custom view can be defined with the following elements:

IConceptViewProvider

To replace the default view by your custom view you can use the IConceptViewProvider interface. This interface forces the implementation of the GetView method. In this example, MachineView is the custom view that you could have created for the instances of the machine class.

public class Machine : ConceptComponent, IConceptViewProvider
{
    public FrameworkElement GetView(string subject = "")
    {
        if (subject == string.Empty)
            return new MachineView() { DataContext = this };
        else
            throw new NotSupportedException(string.Format("No view found for the following subject : '{0}'!", subject));
    }
}

The GetView method has a parameter which gives the ability to define multiple custom views for a component. This parameter is a string called "subject" that will be used to display the adequate view. The example below shows how to use a subject in order to add an additional view of the machine intended for manual operations. In this example, MachineView and MachineManualView are the custom views that have been created for the machine class.

public class Machine : ConceptComponent, IConceptViewProvider
{
    public FrameworkElement GetView(string subject = "")
    {
        if (subject == string.Empty)
            return new MachineView() { DataContext = this };
        else if (subject == MachineManualSubject )
            return new MachineManuelView() { DataContext = this };
        else
            throw new NotSupportedException(string.Format("No view found for the following subject : '{0}'!", subject));
    }
    public const string MachineManualSubject = "MachineManual";
}
With IConceptViewProvider each view is created manually and it is possible to add a custom behavior to initialize each of them.

ConceptView

To define a custom view, Concept also provides the ConceptView attribute which associates a model with its view.

The first way to use it is to put this attribute on the model class or view model class. This attribute requires the type of the view created for the machine class.

[ConceptView(typeof(MachineView))]
public class Machine : ConceptComponent {}

To completely separate the model from the view, the ConceptView attribute can also be used on the view class. In this case, the attribute requires the type of the model class which has to be linked with this view.

[ConceptView(typeof(Machine))]
public partial class MachineView : UserControl
{
    public MachineView()
    {
        InitializeComponent();
    }
}

As for the IConceptViewProvider interface, it is also possible to work with subject to define multiple custom views for a component. The subject is the fourth parameter of the attribute. In this case, the second and the third parameters are not used and must be left empty.

[ConceptView(typeof(MachineView))]
[ConceptView(typeof(MachineManualView), "", "", "MachineManual")]]
public class Machine : ConceptComponent {}
How to display these views

With Concept, the view associated to a model can easily be shown. In order to do so, a ContentControl and a concept converter (ConceptElementToViewConverter) can be used.

The example below shows how to display the default view of the Machine model.

<ContentControl Content="{Binding Path=Machine, Converter={concept:ConceptElementToViewConverter}}"/> 

The example below shows how to display the manual view of the Machine model.

<ContentControl Content="{Binding Path=Machine, Converter={concept:ConceptElementToViewConverter Subject=MachineManual}}"/> 
To easily display and edit a model based on Concept use ConceptEditor .

 

 


© 2016 Objectis

Send Feedback