WPF - 自定义面板大小到内容

时间:2013-11-05 15:58:10

标签: wpf layout panel measureoverride arrangeoverride

我的面板尝试执行类似于UniformGrid的操作,除了ItemContainerWidth(40.0)和ItemContainerHeight(20.0)依赖项属性之外,它还有一个ColumnNumber属性。

我希望我的面板能够根据内容进行调整。

我的MesureOverride:

 protected override Size MeasureOverride(Size constraint)
 {            
     if (constraint.Width == double.PositiveInfinity || constraint.Height == double.PositiveInfinity)
         return Size.Empty;

     for (int i = 0; i < InternalChildren.Count; i++)
     {
         InternalChildren[i].Measure(new Size(ItemContainerWidth, ItemContainerHeight));
     }

     return constraint;
 }

我的ArrangeOverride:

  protected override Size ArrangeOverride(Size finalSize)
  {                  
        int currentColumn = 0; 
        int currentRow = 0 ; 

        for (int i = 0; i < InternalChildren.Count; i++)
        {
            UIElement child = InternalChildren[i];

            if (currentColumn == ColumnCount)
            {
                currentColumn = 0;
                currentRow++;
            }
            currentColumn++; 

            double top = currentRow * ItemContainerHeight;
            double left = currentColumn * ItemContainerWidth;

            child.Arrange(new Rect(left, top, ItemContainerWidth, ItemContainerHeight));
        }

        return finalSize;
    }

我的面板的ActualWidth和Height等于finalSize,我希望面板的大小是从MeasureOvride返回的(约束),

如果我强制这个我的面板在屏幕上的位置不同,我只是希望我的面板大小适合它的内容,如WrapPanel或StackPanel,

使用finalSize

with finalSize

约束大小(100,100)用于测试目的

with constraint size

我对小组的使用:XAML:

<Grid>
   <ItemsControl ItemsSource="{Binding Items, Mode=OneWay}" >            
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <uni:ScrollableUniformGrid  ColumnCount="12" />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>

        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Border BorderBrush="Black" BorderThickness="0,0,1,1">
                    <TextBlock Text="{Binding}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                </Border>
            </DataTemplate>
        </ItemsControl.ItemTemplate>   
    </ItemsControl>
 </Grid>

2 个答案:

答案 0 :(得分:1)

对不起,我想我第一次误解了你。

看看这个:

<Grid>
    <ItemsControl ItemsSource="{Binding}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <local:ScrollableUniformGrid  HorizontalAlignment="Left" VerticalAlignment="Top" ColumnCount="3" />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>

        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Border BorderBrush="Black" BorderThickness="0,0,1,1">
                    <TextBlock Text="{Binding}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                </Border>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Grid>

这就是您的ArrangeOverride的样子。 MeasureOverride可能保持不变。

protected override Size ArrangeOverride(Size finalSize)
{
    int currentColumn = 0;
    int currentRow = 0;


    for (int i = 0; i < InternalChildren.Count; i++)
    {
        UIElement child = InternalChildren[i];

        double top = currentRow * ItemContainerHeight;
        double left = currentColumn * ItemContainerWidth;

        if (currentColumn == ColumnCount)
        {
            currentColumn = 0;
            currentRow++;
        }
        else
        {
            currentColumn++;
        }

        child.Arrange(new Rect(left, top, ItemContainerWidth, ItemContainerHeight));
    }

    return new Size((ColumnCount + 1) * ItemContainerWidth, (currentRow + 1) * ItemContainerHeight);
}

看看这张照片。该小组适合我的内容。

enter image description here

答案 1 :(得分:0)

建立appon是什么开发hedgehod说Grid是父母伸展我的面板, 我没有想到这个我认为ItemsControl是设置我的面板边界的那个。

我最终做了以下操作,我在列和行定义中将宽度和高度设置为自动。

XAML:

 <Grid>

      <Grid.ColumnDefinitions>
          <ColumnDefinition Width="Auto"/>
      </Grid.ColumnDefinitions>

      <Grid.RowDefinitions>
         <RowDefinition Height="Auto" />
      </Grid.RowDefinitions>

      <ItemsControl ItemsSource="{Binding Items, Mode=OneWay}"  Background="AliceBlue">            
          <ItemsControl.ItemsPanel>
              <ItemsPanelTemplate>
                  <uni:ScrollableUniformGrid  ColumnCount="12" />
              </ItemsPanelTemplate>
          </ItemsControl.ItemsPanel>            
       <ItemsControl.ItemTemplate>
           <DataTemplate>
              <Border BorderBrush="Black" BorderThickness="0,0,1,1">
                 <TextBlock Text="{Binding}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
              </Border>
          </DataTemplate>
       </ItemsControl.ItemTemplate>   
   </ItemsControl>

</Grid>