0

我在将 ObservableCollection 绑定到画布时遇到了一些困难。

XAML:

<ScrollViewer HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible">
    <local:DesignerCanvas x:Name="designerCanvas"  AllowDrop="True">
        <ItemsControl ItemsSource="{Binding Circles}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <Canvas />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </ItemsControl>
        <Button Canvas.Left="452" Canvas.Top="487" Content="Button" Height="23" Name="button1" Width="75" Click="button1_Click" />
    </local:DesignerCanvas>
</ScrollViewer>

注意:DesignerCanvas 只是一个从 Canvas 派生的类。我已经覆盖了 MeasureOverride 函数以使其正确调整大小以显示滚动查看器。

代码背后:

public partial class LogicView : UserControl
{
    private ObservableCollection<Shape> circles;
    public ObservableCollection<Shape> Circles 
    {
        get { return circles; }
        set { circles = value; }
    }
    ...

    public void DrawCircle()
    {
        Ellipse ellipse = new Ellipse();

        SolidColorBrush brush = new SolidColorBrush(Brushes.Blue.Color);
        ellipse.Fill = brush;
        ellipse.StrokeThickness = 2;
        ellipse.Stroke = Brushes.Black;

        ellipse.Width = 100;
        ellipse.Height = 100;

        DesignerCanvas.SetLeft(ellipse, 100);
        Circles.Add(ellipse);
    }
}

DrawCircle 函数向 ObservableCollection 添加了一个椭圆,但画布上没有显示任何内容。绑定似乎无法正常工作。可能是什么问题?

4

1 回答 1

1

如果您使用的是 Caliburn Micro,请更换

ItemsSource="{Binding Circles}" 

Name="Circles" 

会解决你的问题

于 2012-09-25T08:10:28.240 回答