Hi JackLin,
If you want to bind a variable to change the style of something within your compound widget, you can define the style as a property.
First, lay your widgets out within a Content. This is what I did:
Then SAVE your content and close it out. Open it as a text file by right-clicking on it in the Logical View and selecting Open → Open As Text. This will show you the XML definition of the content. The important part is what’s in the Widgets tag:
<Widgets>
<Widget xsi:type="widgets.brease.Rectangle" id="Rectangle1" top="260" left="360" zIndex="0" style="Orange_backColor" />
<Widget xsi:type="widgets.brease.TextOutput" id="TextOutput1" top="280" left="520" width="220" height="60" zIndex="1" style="Output" />
</Widgets>
Paste this directly into the Widgets tag in your compound widget. This will define your compound widget as the collection of a Rectangle widget and a Text Output widget. You could then build your project and use this new widget as is, but it wouldn’t have any bindable properties. To add those, update the Properties section of the compound widget using the template included when you import a new compound widget:
<Property xsi:type="BindableProperty" name="TextToShow" type="String" required="false" defaultValue="" readOnly="false">
<Description>Text which is shown in the TextOutput widget</Description>
<Mappings>
<Mapping widget="TextOutput1" property="value" mode="oneWay"/>
</Mappings>
</Property>
<Property xsi:type="BindableProperty" name="RectangleStyle" type="String" defaultValue="" readOnly="false" typeRefId="widgets.brease.Rectangle">
<Description>Style of the Rectangle widget</Description>
<Mappings>
<Mapping widget="Rectangle1" property="style" mode="oneWay"/>
</Mappings>
</Property>
This code will create two bindable properties which will then show up in the widget’s properties once you add it to a Content. These properties can then be bound to variables:
Note that when you change a widget’s appearance with a Style, that Style needs to be present in the project and is defined within a Theme. If your theme or style changes, the value you’re sending the widget may need to be updated. For this reason, it may be easier to use an ImageList widget and instead create images of different colored rectangles that you can cycle between (depending of course on the use case).
If you want to change the style using an Eventbinding (i.e. by changing a BOOL in your code), you would instead define the style change as an Action so you don’t need to include the “RectangleStyle” bindable property above (though you may still want the text one). Like a BindableProperty, an Action in a compound widget simply creates an Action for your widget that links to an existing Action for one of the sub-widgets. In this case, you’d want to link to the SetStyle action for a Rectangle widget. You can do this by adding the following code to the Action tag:
<Action name="ChangeBoxColor">
<Description>This action will change the color of the box.</Description>
<Mappings>
<Mapping widget="Rectangle1" action="SetStyle" />
</Mappings>
</Action>
You can then link an event to the ChangeBoxColor action like you would link an event to the SetStyle action of a Rectangle.
This is a quick example I made that sets the color to Blue when my BOOL turns true and sets the color to Orange when the BOOL turns false:
<EventBinding id="EventBinding_1">
<Source xsi:type="opcUa.Event" refId="::Program:ChangeStyle" event="ValueChanged" />
<EventHandler condition="newValue">
<Action>
<Target xsi:type="widgets.widgetlibrary_0.compoundwidget_0.Action" contentRefId="content_0" widgetRefId="compoundwidget_01">
<Method xsi:type="widgets.widgetlibrary_0.compoundwidget_0.Action.ChangeBoxColor" value="Blue_backColor" />
</Target>
</Action>
</EventHandler>
</EventBinding>
<EventBinding id="EventBinding_2">
<Source xsi:type="opcUa.Event" refId="::Program:ChangeStyle" event="ValueChanged" />
<EventHandler condition="NOT newValue">
<Action>
<Target xsi:type="widgets.widgetlibrary_0.compoundwidget_0.Action" contentRefId="content_0" widgetRefId="compoundwidget_01">
<Method xsi:type="widgets.widgetlibrary_0.compoundwidget_0.Action.ChangeBoxColor" value="Orange_backColor" />
</Target>
</Action>
</EventHandler>
</EventBinding>
You said that you’re having trouble using OpcUa events with compound widgets. I’m not sure why that’s the case. As far as I know there is no such restriction, and I got the above example working with mappView version 5.24. Otherwise, once you’ve created an Action within your compound widget, you should be able to use it as you already have been doing with the Rectangle widget.