What I want to be more specific is an element I can use for grouping a set of other elements, without effecting their layout. The only thing it should do besides giving a nicer XAML by grouping related elements in their own parent tag is to propagate ambient properties such as DataContext. It should be a purely logical element without any visual. Is there something in WPF/XAML intended to be used like this?
There's no direct equivalent of HTML's <div>
in WPF/XAML, however, it sounds like the closest thing is WPF's Panel
Class:
Provides a base class for all Panel elements. Use Panel elements to position and arrange child objects in Windows Presentation Foundation (WPF) applications.
However, the Panel
class is an abstract class which cannot be instantiated directly, and can only be used by utilising one of the non-abstract derivations within the WPF framework. These include:
Canvas, DockPanel, Grid, TabPanel, ToolBarOverflowPanel, UniformGrid, StackPanel, VirtualizingPanel, WrapPanel
From what you are trying to achieve, the closest of these to your needs would probably be either the Canvas
or Grid
classes.
If neither of these classes are appropriate, you are always free to implement your own class deriving from the Panel
class.
Bear in mind, though, that all of these classes (including the base Panel
class) are, by their very nature, UI-specific (i.e. they are intended to be visual elements rather than purely logical) although they can be configured to have little or no visible visual element to them. This is entirely by design in WPF as the Wikipedia article on XAML correctly states:
In WPF, XAML is used as a user interface markup language to define UI elements, data binding, eventing, and other features.
XAML, unlike HTML, does not claim to be "semantic" - it's a presentation technology and it knows it's a presentation technology, there are no XAML elements that only exist to make the markup look nicer without changing the display.
You can't implement a WPF element like you want because any change to the structure of the XAML will change the layout, for example, a Grid (like all panels) will arrange it's direct children - the moment you group some of those children inside another element the Grid will layout that element as a single unit and not each of it's children individually.
This makes sense if you think of XAML as a way to fill .net objects, a Grid has a Children property, that property is a list, and it will layout the items of that list.