我将如何绑定到一组形状?绑定到一组形状?
我想建立一个小型应用程序(仅用于学习目的),我利用MVVM绘制形状。
的主窗口的DataContext的是MainWindowViewModel
即MainWindowViewModel具有形状的一个ObservableCollection。
我目前只能在我的主窗口与它的DataContext绑定到该集合的画布不工作:
<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>
在MainWindowViewModel的构造我添加一个矩形这样的:
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);
目前没有绑定错误。有任何想法吗?