How would I bind to a collection of shapes?
I'd like to build a small application (just for learning purposes) where I utilize MVVM for drawing shapes.
The DataContext of the MainWindow is the MainWindowViewModel
That MainWindowViewModel has an ObservableCollection of shapes.
I have currently only a Canvas on my MainWindow with its DataContext bound to that collection which does not work:
<Window x:Class="DesktopCanvas.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
<Canvas DataContext="{Binding Source=ShapeCollection}">
</Canvas>
</Window>
In the constructor of the MainWindowViewModel I add a rectangle like this:
this.ShapeCollection = new ObservableCollection<Shape>();
Rectangle rect = new Rectangle();
//Größe
rect.Height = 75;
rect.Width = 75;
//Transparenz
rect.Opacity = 100;
//Farbe
SolidColorBrush myBrush = new SolidColorBrush(Colors.Red);
rect.Fill = myBrush;
this.ShapeCollection.Add(rect);
No Binding errors so far. Any ideas?