我將如何綁定到一組形狀?綁定到一組形狀?
我想建立一個小型應用程序(僅用於學習目的),我利用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);
目前沒有綁定錯誤。有任何想法嗎?