1

我需要为地铁创建 UniformGrid 模拟。

我写了这个

public class UniformGrid : Panel
{
    public static readonly DependencyProperty HorizontalCountProperty =
        DependencyProperty.Register("HorizontalCount", typeof (int), typeof (UniformGrid),
                                    new PropertyMetadata(default(int)));

    public int HorizontalCount
    {
        get { return (int) GetValue(HorizontalCountProperty); }
        set { SetValue(HorizontalCountProperty, value); }
    }

    public static readonly DependencyProperty ElementsGapProperty =
        DependencyProperty.Register("ElementsGap", typeof (double), typeof (UniformGrid),
                                    new PropertyMetadata(default(double)));

    public double ElementsGap
    {
        get { return (double) GetValue(ElementsGapProperty); }
        set { SetValue(ElementsGapProperty, value); }
    }

    protected override Size MeasureOverride(Size availableSize)
    {
        return new Size();
    }

    protected override Size ArrangeOverride(Size finalSize)
    {
        if (Children != null && Children.Count != 0)
        {
            var squareSideForElement = (finalSize.Width - (HorizontalCount - 1)*ElementsGap)/HorizontalCount;
            var sizeOfElement = new Size(squareSideForElement, squareSideForElement);
            for (var i = 0; i < Children.Count; i++)
            {
                var rowIndex = i%HorizontalCount;
                var columnIndex = i/HorizontalCount;
                var resultPoint = new Point
                    {
                        X = rowIndex*(squareSideForElement + ElementsGap),
                        Y = columnIndex*(squareSideForElement + ElementsGap)
                    };
                Children[i].Arrange(new Rect(resultPoint, sizeOfElement));

            }

        }
        return finalSize;
    }
}

但是在我明白我需要从 ItemsControl 派生的控制权之后,因为我需要 ItemTemplate、ItemsSource 等。

我做的

public class UniformGrid : ItemsControl
{
    public static readonly DependencyProperty HorizontalCountProperty =
        DependencyProperty.Register("HorizontalCount", typeof (int), typeof (UniformGrid),
                                    new PropertyMetadata(default(int)));

    public int HorizontalCount
    {
        get { return (int) GetValue(HorizontalCountProperty); }
        set { SetValue(HorizontalCountProperty, value); }
    }

    public static readonly DependencyProperty ElementsGapProperty =
        DependencyProperty.Register("ElementsGap", typeof (double), typeof (UniformGrid),
                                    new PropertyMetadata(default(double)));

    public double ElementsGap
    {
        get { return (double) GetValue(ElementsGapProperty); }
        set { SetValue(ElementsGapProperty, value); }
    }

    protected override Size MeasureOverride(Size availableSize)
    {
        return new Size();
    }

    protected override Size ArrangeOverride(Size finalSize)
    {
        if (Items != null && Items.Count != 0)
        {
            var squareSideForElement = (finalSize.Width - (HorizontalCount - 1)*ElementsGap)/HorizontalCount;
            var sizeOfElement = new Size(squareSideForElement, squareSideForElement);
            for (var i = 0; i < Items.Count; i++)
            {
                var rowIndex = i%HorizontalCount;
                var columnIndex = i/HorizontalCount;
                var resultPoint = new Point
                    {
                        X = rowIndex*(squareSideForElement + ElementsGap),
                        Y = columnIndex*(squareSideForElement + ElementsGap)
                    };
                Items[i].Arrange(new Rect(resultPoint, sizeOfElement));

            }

        }
        return finalSize;
    }
}

但我有错误在线

Items[i].Arrange(new Rect(resultPoint, sizeOfElement));

我理解这个错误的原因。

我的问题是

1)如何获取这个项目的项目模板,其中填充了绑定提供的数据?

2)是否需要为MeasureOverride中的每个项目调用Measure?

4

1 回答 1

2

就像是:

<ItemsControl ItemsSource="{Binding Items}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <!-- your item template-->
                </DataTemplate>
            </ItemsControl.ItemTemplate>
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <UniformGrid/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </ItemsControl>
于 2012-12-10T08:18:22.340 回答