WPF - IScrollInfo实现

时间:2011-11-10 23:20:44

标签: wpf scroll itemscontrol measureoverride arrangeoverride

问题:一个具有ItemsControl和一些项目的窗口(比如说矩形)。 窗口设置了MinWidth和MinHeight(例如300) 如果矩形没有足够的空间显示在2列中,我需要在调整窗口大小时调整窗口大小。 如果在2列中仍然没有空间来显示滚动查看器。

我尝试过的: 1.创建扩展的ItemsControl:

<ItemsControl.ItemsPanel>
    <ItemsPanelTemplate>
        <local:MyGrid IsItemsHost="True" x:Name="PART_ItemsPanel" Initialized="OnItemsPanelInitialized" CanVerticallyScroll="True" CanHorizontallyScroll="True">
            <local:MyGrid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
            </local:MyGrid.ColumnDefinitions>
            <local:MyGrid.RowDefinitions>
                <RowDefinition Height="auto"/>
                <RowDefinition Height="auto"/>
                <RowDefinition Height="auto"/>
                <RowDefinition Height="auto"/>
            </local:MyGrid.RowDefinitions>
        </local:MyGrid>
    </ItemsPanelTemplate>
</ItemsControl.ItemsPanel>

我的ItemsControl具有ItemsPanelTemplate扩展网格:

public class MyGrid:Grid,IScrollInfo     {            .... IScrollInfo实现     }

我使用这个网格认为当ItemsControl将PrepareContainerForItemOverride()时,我可以使用它来分割两列中的Items。

ideea是从会议中“采取”的......但我不知道下一步该做什么...... 我有类似的问题: 当我覆盖测量和排列数据网格时,我在DataGrid中设置项目的位置,但之后它被称为PrepareContainerForItemOverride()...那么什么?我应该计算出我应该做的行数吗?但如果那时我再次调整窗口大小...将不会调用PrepareContainerForItemOverride()......

这个问题在我身上......请告诉我一些人是否有一个。 谢谢你们!

2 个答案:

答案 0 :(得分:0)

在我看来,您所需要的只是ItemsControl中不同的面板。你可能会使用内置的WrapPanel,但你也可以自己编写:

<ItemsControl ...>
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <!-- put whatever panel that encapsulates your layout logic here, possibly your own -->
            <WrapPanel/>
        </ItemsPanelTemplate>
    <ItemsControl.ItemsPanel>
</ItemsControl>

答案 1 :(得分:0)

我想分享我采取的解决方案......如果有人需要它。

为了避免IScrollInfo的实现,我使用ListBox控件更改了ItemsControl。所以我仍然重写安排。 在安排我计算我的第一列的项目高度,我在GridPanel中安排项目。我使用调度程序,因为scrollViewer排列并不总是有效,我依靠它来获得可见高度。

<ListBox x:Uid="allListBox" x:Name="adminListBoxControl" ItemTemplate ="{DynamicResource LinkButtonItemTemplate}" Margin="15" BorderBrush="Transparent" Background="Transparent">
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <local:GridPanel>
                        <local:GridPanel.ColumnDefinitions>
                            <ColumnDefinition Width="0.45*"/>
                            <ColumnDefinition Width="0.45*"/>
                            <ColumnDefinition Width="0.1*"/>
                        </local:GridPanel.ColumnDefinitions>
                        <local:GridPanel.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                        </local:GridPanel.RowDefinitions>
                    </local:GridPanel>
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
        </ListBox>

protected override Size ArrangeOverride(Size finalSize)
    {
       Size arrangeSize = base.ArrangeOverride(finalSize);

        lastItemInFirstColumn = -1;
        double totalHeight = 0;

        DispatcherPriority p = ScrollViewer.IsArrangeValid ? DispatcherPriority.Normal : DispatcherPriority.Input;
        if (o != null)
            o.Abort();

        o = Dispatcher.BeginInvoke(p, (Action)(() =>
        {
            double fitHeight = ScrollViewer.ActualHeight;
            foreach (UIElement child in this.InternalChildren)
            {
                if (totalHeight + child.DesiredSize.Height < fitHeight)
                {
                    totalHeight += child.DesiredSize.Height;
                    lastItemInFirstColumn++;
                }
                else
                {
                    if (lastItemInFirstColumn + 1 < InternalChildren.Count - (lastItemInFirstColumn + 1))
                        lastItemInFirstColumn++;
                    break;
                }
            }

            //set items positions
            ArrangeItemsInGrid();
        }));

        o.Completed += (s, e) => { o = null; };
        return arrangeSize;
    }