It is in fact exactly what the error says. You need to put the widget inside of a datacontainer.
In this example, the test text widget is outside of a container, resulting in the error.
If I put it inside the Dashboard the error is gone
Furthermore, I can now use any attribute in my Dashboard object (referred to as $currentObject) to display the required information like
if $currentObject/IsEditModeActive then 'disabled' else ''
You need to point to the npe from the expression on the page. To this by adding a page parameter or adding a dataview to the page where the element holding the dynamic expression is in.
Rather than using dynamic classes, you may find it easier to use the Visibility section on the widgets and other elements on the page.
https://docs.mendix.com/refguide9/common-widget-properties/#visibility-properties
If you want to conditionally display content, this is usually the best method.
Good luck!