public class MyWrapPanel : Panel
{
}
protected override Size MeasureOverride(Size availableSize)
{
return base.MeasureOverride(availableSize);
}
protected override Size ArrangeOverride(Size finalSize)
{
return base.ArrangeOverride(finalSize);
}
protected override Size MeasureOverride(Size availableSize)
{
double x = 0.0;
double y = 0.0;
double largestY = 0.0;
foreach (FrameworkElement child in this.InternalChildren)
{
child.Measure(availableSize);
}
foreach (FrameworkElement child in this.InternalChildren)
{
if (x + child.DesiredSize.Width > availableSize.Width)
{
if (x > 0)
{
x = 0;
y = largestY;
x += child.DesiredSize.Width;
}
else
{
x += child.DesiredSize.Width;
}
}
else
{
x += child.DesiredSize.Width;
}
largestY = Math.Max(largestY, y + child.DesiredSize.Height);
}
return availableSize;
}
protected override Size ArrangeOverride(Size finalSize)
{
double x = 0.0;
double y = 0.0;
double largestY = 0.0;
foreach (FrameworkElement child in this.InternalChildren)
{
if (x + child.DesiredSize.Width > finalSize.Width)
{
if (x > 0)
{
x = 0;
y = largestY;
child.Arrange(new Rect(new Point(x, y), child.DesiredSize));
x += child.DesiredSize.Width;
}
else
{
child.Arrange(new Rect(new Point(x, y), child.DesiredSize));
x += child.DesiredSize.Width;
}
}
else
{
child.Arrange(new Rect(new Point(x, y), child.DesiredSize));
x += child.DesiredSize.Width;
}
largestY = Math.Max(largestY, y + child.DesiredSize.Height);
}
return new Size(finalSize.Width, largestY);
}
<Window x:Class="WrapPanelDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WrapPanelDemo"
Title="MainWindow" Height="450" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<WrapPanel>
<Button Width="50" Height="50" VerticalAlignment="Top">1</Button>
<Button Width="50" Height="50" VerticalAlignment="Top">2</Button>
<Button Width="50" Height="50" VerticalAlignment="Top">3</Button>
<Button Width="50" Height="50" VerticalAlignment="Top">4</Button>
<Button Width="50" Height="50" VerticalAlignment="Top">5</Button>
<Button Width="150" Height="150" VerticalAlignment="Top">6</Button>
<Button Width="50" Height="50" VerticalAlignment="Top">7</Button>
<Button Width="50" Height="50" VerticalAlignment="Top">8</Button>
<Button Width="50" Height="50" VerticalAlignment="Top">9</Button>
<Button Width="50" Height="50" VerticalAlignment="Top">10</Button>
<Button Width="50" Height="50" VerticalAlignment="Top">11</Button>
<Button Width="50" Height="50" VerticalAlignment="Top">12</Button>
<Button Width="50" Height="50" VerticalAlignment="Top">13</Button>
<Button Width="50" Height="50" VerticalAlignment="Top">14</Button>
</WrapPanel>
<local:MyWrapPanel Grid.Row="1">
<Button Width="50" Height="50" VerticalAlignment="Top">1</Button>
<Button Width="50" Height="50" VerticalAlignment="Top">2</Button>
<Button Width="50" Height="50" VerticalAlignment="Top">3</Button>
<Button Width="50" Height="50" VerticalAlignment="Top">4</Button>
<Button Width="50" Height="50" VerticalAlignment="Top">5</Button>
<Button Width="150" Height="150" VerticalAlignment="Top">6</Button>
<Button Width="50" Height="50" VerticalAlignment="Top">7</Button>
<Button Width="50" Height="50" VerticalAlignment="Top">8</Button>
<Button Width="50" Height="50" VerticalAlignment="Top">9</Button>
<Button Width="50" Height="50" VerticalAlignment="Top">10</Button>
<Button Width="50" Height="50" VerticalAlignment="Top">11</Button>
<Button Width="50" Height="50" VerticalAlignment="Top">12</Button>
<Button Width="50" Height="50" VerticalAlignment="Top">13</Button>
<Button Width="50" Height="50" VerticalAlignment="Top">14</Button>
</local:MyWrapPanel>
</Grid>
</Window>
<Window x:Class="WrapPanelDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WrapPanelDemo"
Title="MainWindow" Height="450" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<ScrollViewer>
<WrapPanel>
<Button Width="50" Height="50" VerticalAlignment="Top">1</Button>
<Button Width="50" Height="50" VerticalAlignment="Top">2</Button>
<Button Width="50" Height="50" VerticalAlignment="Top">3</Button>
<Button Width="50" Height="50" VerticalAlignment="Top">4</Button>
<Button Width="50" Height="50" VerticalAlignment="Top">5</Button>
<Button Width="150" Height="150" VerticalAlignment="Top">6</Button>
<Button Width="50" Height="50" VerticalAlignment="Top">7</Button>
<Button Width="50" Height="50" VerticalAlignment="Top">8</Button>
<Button Width="50" Height="50" VerticalAlignment="Top">9</Button>
<Button Width="50" Height="50" VerticalAlignment="Top">10</Button>
<Button Width="50" Height="50" VerticalAlignment="Top">11</Button>
<Button Width="50" Height="50" VerticalAlignment="Top">12</Button>
<Button Width="50" Height="50" VerticalAlignment="Top">13</Button>
<Button Width="50" Height="50" VerticalAlignment="Top">14</Button>
</WrapPanel>
</ScrollViewer>
<ScrollViewer Grid.Row="1">
<local:MyWrapPanel >
<Button Width="50" Height="50" VerticalAlignment="Top">1</Button>
<Button Width="50" Height="50" VerticalAlignment="Top">2</Button>
<Button Width="50" Height="50" VerticalAlignment="Top">3</Button>
<Button Width="50" Height="50" VerticalAlignment="Top">4</Button>
<Button Width="50" Height="50" VerticalAlignment="Top">5</Button>
<Button Width="150" Height="150" VerticalAlignment="Top">6</Button>
<Button Width="50" Height="50" VerticalAlignment="Top">7</Button>
<Button Width="50" Height="50" VerticalAlignment="Top">8</Button>
<Button Width="50" Height="50" VerticalAlignment="Top">9</Button>
<Button Width="50" Height="50" VerticalAlignment="Top">10</Button>
<Button Width="50" Height="50" VerticalAlignment="Top">11</Button>
<Button Width="50" Height="50" VerticalAlignment="Top">12</Button>
<Button Width="50" Height="50" VerticalAlignment="Top">13</Button>
<Button Width="50" Height="50" VerticalAlignment="Top">14</Button>
</local:MyWrapPanel>
</ScrollViewer>
</Grid>
</Window>
protected override Size MeasureOverride(System.Windows.Size availableSize)
{
double x = 0.0;
double y = 0.0;
double largestY = 0.0;
double largestX = 0.0;
foreach (FrameworkElement child in this.InternalChildren)
{
child.Measure(availableSize);
}
foreach (FrameworkElement child in this.InternalChildren)
{
if (x + child.DesiredSize.Width > availableSize.Width)
{
if (x > 0)
{
x = 0;
y = largestY;
x += child.DesiredSize.Width;
}
else
{
x += child.DesiredSize.Width;
}
}
else
{
x += child.DesiredSize.Width;
}
largestY = Math.Max(largestY, y + child.DesiredSize.Height);
largestX = Math.Max(largestX, x + child.DesiredSize.Width);
}
return new Size(largestX, largestY);
}
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)