MVVM パターンを使用して WPF アプリケーションを開発しています。私のアプリケーションには、いくつかの異なる形状を表示するキャンバスがあります。これらの形状は、倉庫 (別名ストレージ) と倉庫の内容を表します。
倉庫 ( で記述されているObservableCollection<Point>
) のみを表示するには、次のコード スニペットを使用します。
<Canvas Width="{Binding StorageWidth, Mode=OneWay}" Height="{Binding StorageHeight, Mode=OneWay}">
<Polygon Points="{Binding StorageVertices, Mode=OneWay, Converter={StaticResource PointCollectionConverter}}" Stroke="Gray" StrokeThickness="30">
<Polygon.Fill>
<SolidColorBrush Color="DarkRed" Opacity="0.25" />
</Polygon.Fill>
</Polygon>
</Canvas>
このキャンバスに、長方形 (ストレージ内のオフィスを表すため) と円 (ストレージ内のノードを表すため) を追加します。これらは私のビューモデルで定義されています:
class Node
{
// ...
Point Position { get; private set; }
}
class Office
{
// ...
ObservableCollection<Point> Vertices { get; private set; }
}
public class ViewModel : ViewModelBase
{
// ...
ObservableCollection<Node> Nodes { get; private set; }
ObservableCollection<Office> Offices { get; private set; }
ObservableCollection<Point> StorageVertices { get; private set; } // Already displayed on the canvas
}
これらをデータ バインディングを使用してストレージ領域と一緒にキャンバスに表示するにはどうすればよいですか? 通常どおり使用できたことはわかっていItemsControl
ますが、今ではいくつかの異なるコレクション/ソースがあり、それらはさまざまな方法で記述されることになっています ( Node
s はCircle
sですが、 Office
s はRectangle
s です)。