0
我想在一個Canvas中顯示不同的形狀(我在多個Canvases上有一個解決方案 - 但這不能讓我選擇所有形狀,所以它是毫無價值的)。我把我的形狀放在CompositeCollection中,並在一個ItemsControl中使用多個DataTemplates。但是,程序不會顯示形狀,而是在位置X,Y顯示屬性名稱。顯示WPF中形狀的CompositeCollector
這是集合:
Page.Collection.Add(new CollectionContainer() { Collection = Page.Lines });
Page.Collection.Add(new CollectionContainer() { Collection = Page.Rectangles });
Page.Collection.Add(new CollectionContainer() { Collection = Page.Circles });
這是類頁面的一部分:
public class Page:ViewModelBase,INotifyPropertyChanged
{
ObservableCollection<Line> lines = new ObservableCollection<Line>();
ObservableCollection<Rectangle> rectangles = new ObservableCollection<Rectangle>();
ObservableCollection<Circle> circles = new ObservableCollection<Circle>();
CompositeCollection collection = new CompositeCollection();
public CompositeCollection Collection
{
get
{
return collection;
}
set
{
collection = value;
OnPropertyChanged("Collection");
}
}
public ObservableCollection<Line> Lines
{
get
{
return lines;
}
set
{
lines = value;
OnPropertyChanged("Lines");
}
}
這裏是XAML:
<ItemsControl ItemsSource="{Binding Page.Collection}" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas IsItemsHost="True" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
Background="Transparent">
</Canvas>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="Canvas.Left" Value="{Binding Start.X}"/>
<Setter Property="Canvas.Top" Value="{Binding Start.Y}"/>
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.Resources>
<DataTemplate DataType="Page.Lines">
<Line X1="{Binding Start.X}"
Y1="{Binding Start.Y}"
X2="{Binding End.X}"
Y2="{Binding End.Y}" Stroke="Black" StrokeThickness="1" />
</DataTemplate>
<DataTemplate DataType="Page.Rectangles">
<Rectangle
Width="{Binding Extend.X}"
Height="{Binding Extend.Y}" Stroke="Black" StrokeThickness="1" />
</DataTemplate>
<DataTemplate DataType="Page.Circles">
<Ellipse
Width="{Binding Extend.X}"
Height="{Binding Extend.Y}" Stroke="Black" StrokeThickness="1" />
</DataTemplate>
</ItemsControl.Resources>
</ItemsControl>
命名空間'數據類型'必須是一個類型['{x:Type somenamespace:Line}'](https://msdn.microsoft.com/en-us/library/ms753322%28v=vs.110%29.aspx?f=255&MSPPError= -2147217396),否則會被處理作爲XPath – dkozl
請將此作爲答案,然後我將其標記爲答案。 – laserman