I need to display a collection of RectangleGeometry rectangles that would be added and removed in a ViewModel so I created an ObservableCollection of RectangleGeometry objects and bind to them inside a GeometryGroup.
To make my problem really simple to see, I created a very simple project without the MVVM. Even if I bind to the simple code-behind, the rectangles do not appear. I'm still in WPF learning mode, so if you have a better way of doing this using templates or something else, I'm very open.
The xaml:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfApplication1"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="MainWindow"
Width="1024"
Height="768"
mc:Ignorable="d">
<Grid>
<Canvas>
<Path Fill="Red" Opacity=".4">
<Path.Data>
<GeometryGroup Children="{Binding Rectangles}"
FillRule="NonZero" />
</Path.Data>
</Path>
</Canvas>
</Grid>
</Window>
Code-behind:
Imports System.Collections.ObjectModel
Class MainWindow
Public Sub New()
InitializeComponent()
Rectangles = New ObservableCollection(Of RectangleGeometry)
Rectangles.Add(New RectangleGeometry(New Rect(50, 50, 500, 100)))
Rectangles.Add(New RectangleGeometry(New Rect(50, 50, 100, 500)))
Rectangles.Add(New RectangleGeometry(New Rect(200, 200, 100, 100)))
Rectangles.Add(New RectangleGeometry(New Rect(400, 400, 100, 100)))
Rectangles.Add(New RectangleGeometry(New Rect(20, 20, 100, 100)))
End Sub
Public Property Rectangles As ObservableCollection(Of RectangleGeometry)
End Class