スコットはかなり近いですが、そこには完全ではありません。グリッドのDataContextを設定しても、含まれているCircleオブジェクトはレンダリングされません。必要なのは、独自のアイテムをレンダリングし、そのコントロールのItemsSourceプロパティをCircleListにバインドできる埋め込みコントロールです。
これを示す元のクラスを使用して例を作成しました。コードビハインドは次のとおりです。
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
Square square = new Square();
square.CircleList = new List<Circle>() { new Circle(25) };
_shapes.Add(square);
}
private List<Shape> _shapes = new List<Shape>();
public List<Shape> Shapes
{
get { return _shapes; }
}
}
public abstract class Shape { }
public class Circle : Shape
{
public double Diameter { get; private set; }
public Circle(double diameter)
{
Diameter = diameter;
}
}
public class Square : Shape
{
public List<Circle> CircleList { get; set; }
}
したがって、直径25の円を含む単一の正方形をシェイプリストに追加したことがわかります。これは、絶対座標を使用してシェイプを配置するためのサポートを追加しないことに注意してください。私はあなたがすでにそのための何かを持っていると思います。
XAML:
<Window x:Class="TestWpfApplication.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestWpfApplication"
Title="Window1"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Window.Resources>
<DataTemplate DataType="{x:Type local:Circle}">
<Ellipse Stroke="Black"
Width="{Binding Diameter}"
Height="{Binding Diameter}"/>
</DataTemplate>
<DataTemplate DataType="{x:Type local:Square}">
<Border BorderThickness="1" BorderBrush="Black">
<ItemsControl ItemsSource="{Binding CircleList}"/>
</Border>
</DataTemplate>
</Window.Resources>
<ListBox ItemsSource="{Binding Shapes}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<Canvas/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
これがあなたのDataTemplatesです。円が楕円で単純にレンダリングされていることがわかります。一方、Squareには、含まれているアイテムをレンダリングするItemsControlが埋め込まれています。また、その周りにボーダーを描いて正方形にしました。
結果は次のとおりです。
代替テキストhttp://img212.imageshack.us/img212/8658/squarewithcirclecontent.png