Canvas ItemsControl的问题

tho*_*asc 2 wpf itemscontrol observablecollection mvvm

我正在尝试实现此处显示的示例[ WPF Canvas,如何使用MVVM代码动态添加子项 ],但是当我启动程序时(即使IsItemHost为Canvas设置为True)也没有显示.

我的应用程序由实体类型组成:

public class Entity
{
    public Entity(int x, int y, int width, int height)
    {
        X = x;
        Y = y;
        Width = width;
        Height = height;
    }

    public int X { get; set; }
    public int Y { get; set; }
    public int Width { get; set; }
    public int Height { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

实体存储在EntitiesCollection中:

public class EntitiesCollection : ObservableCollection<Entity>
{
    public EntitiesCollection()
    {
        Add(new Entity(10, 10, 10, 10));
        Add(new Entity(50, 20, 25, 25));
    }
}
Run Code Online (Sandbox Code Playgroud)

Wich是DrawingViewModel类的成员:

public class DrawingViewModel
{
    public DrawingViewModel()
    {
        Entities = new EntitiesCollection();
    }

    public EntitiesCollection Entities;
}
Run Code Online (Sandbox Code Playgroud)

我的应用程序的DataContext在MainWindow.xaml.cs中设置:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = new DrawingViewModel();
    }
}
Run Code Online (Sandbox Code Playgroud)

MainWindow.xaml文件本身如下所示:

<Window x:Class="Test.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Test" Height="350" Width="525" Icon="Graphics/Icons/paint.png">
    <Grid>
        <ItemsControl ItemsSource="{Binding Entities}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <Canvas IsItemsHost="True"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemContainerStyle>
                <Style>
                    <Setter Property="Canvas.Left" Value="{Binding X}"/>
                    <Setter Property="Canvas.Top" Value="{Binding Y}"/>
                </Style>
            </ItemsControl.ItemContainerStyle>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Border BorderBrush="Red" BorderThickness="1" Width="{Binding Width}" Height="{Binding Height}"/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

怎么了 ?谢谢.

H.B*_*.B. 5

Entities必须是一个财产而不是一个领域.(应该有相应的绑定错误.)