Skip to content

Commit

Permalink
feat: 支持 StackPanel 在元素 Visibility 为 Collapsed 的时候, Spacing 正常计算
Browse files Browse the repository at this point in the history
  • Loading branch information
SlimeNull committed Apr 15, 2024
1 parent 193bd94 commit 0d1ad4c
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 35 deletions.
2 changes: 1 addition & 1 deletion EleCho.WpfSuite/Panels/FlexPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public double CrossSpacing

public static readonly DependencyProperty ItemsAlignmentProperty =
DependencyProperty.Register(nameof(ItemsAlignment), typeof(FlexItemsAlignment), typeof(FlexPanel),
new FrameworkPropertyMetadata(default(FlexItemsAlignment), FrameworkPropertyMetadataOptions.AffectsMeasure));
new FrameworkPropertyMetadata(FlexItemsAlignment.Stretch, FrameworkPropertyMetadataOptions.AffectsMeasure));

public static readonly DependencyProperty UniformGrowProperty =
DependencyProperty.Register(nameof(UniformGrow), typeof(double), typeof(FlexPanel),
Expand Down
15 changes: 11 additions & 4 deletions EleCho.WpfSuite/Panels/StackPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ protected override Size MeasureOverride(Size availableSize)
var childDesiredSize = child.DesiredSize;

panelDesiredSize.Height += childDesiredSize.Height;
panelDesiredSize.Height += spacing;

if (child.Visibility != Visibility.Collapsed)
panelDesiredSize.Height += spacing;

if (childDesiredSize.Width > panelDesiredSize.Width)
panelDesiredSize.Width = childDesiredSize.Width;
Expand All @@ -54,7 +56,9 @@ protected override Size MeasureOverride(Size availableSize)
var childDesiredSize = child.DesiredSize;

panelDesiredSize.Width += childDesiredSize.Width;
panelDesiredSize.Width += spacing;

if (child.Visibility != Visibility.Collapsed)
panelDesiredSize.Width += spacing;

if (childDesiredSize.Height > panelDesiredSize.Height)
panelDesiredSize.Height = childDesiredSize.Height;
Expand Down Expand Up @@ -83,7 +87,9 @@ protected override Size ArrangeOverride(Size finalSize)
child.Arrange(new Rect(0, offset, finalSize.Width, childDesiredSize.Height));

offset += childDesiredSize.Height;
offset += spacing;

if (child.Visibility != Visibility.Collapsed)
offset += spacing;
}
}
else
Expand All @@ -95,7 +101,8 @@ protected override Size ArrangeOverride(Size finalSize)
child.Arrange(new Rect(offset, 0, childDesiredSize.Width, finalSize.Height));

offset += childDesiredSize.Width;
offset += spacing;
if (child.Visibility != Visibility.Collapsed)
offset += spacing;
}
}

Expand Down
26 changes: 19 additions & 7 deletions EleCho.WpfSuite/Panels/WrapPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ protected override Size MeasureOverride(Size availableSize)
var childHeight = childHeightGetter.Invoke(childDesiredSize);

offsetX += childWidth;
offsetX += horizontalSpacing;

if (child.Visibility != Visibility.Collapsed)
offsetX += horizontalSpacing;

if (offsetX > availableSize.Width)
{
Expand Down Expand Up @@ -113,11 +115,13 @@ protected override Size MeasureOverride(Size availableSize)
var childHeight = childHeightGetter.Invoke(childDesiredSize);

offsetY += childHeight;
offsetY += verticalSpacing;

if (child.Visibility != Visibility.Collapsed)
offsetY += verticalSpacing;

if (offsetY > availableSize.Height)
{
currentLineLength = offsetY -horizontalSpacing - childHeight - verticalSpacing;
currentLineLength = offsetY - horizontalSpacing - childHeight - verticalSpacing;
if (currentLineLength > maxLineLength)
maxLineLength = currentLineLength;

Expand Down Expand Up @@ -171,7 +175,9 @@ protected override Size ArrangeOverride(Size finalSize)
var childHeight = childHeightGetter.Invoke(childDesiredSize);

tempOffset += childWidth;
tempOffset += horizontalSpacing;

if (child.Visibility != Visibility.Collapsed)
tempOffset += horizontalSpacing;

if (tempOffset - horizontalSpacing > finalSize.Width)
{
Expand Down Expand Up @@ -224,7 +230,9 @@ protected override Size ArrangeOverride(Size finalSize)
var childHeight = childHeightGetter.Invoke(childDesiredSize);

tempOffset += childHeight;
tempOffset += verticalSpacing;

if (child.Visibility != Visibility.Collapsed)
tempOffset += verticalSpacing;

if (tempOffset - verticalSpacing > finalSize.Height)
{
Expand Down Expand Up @@ -293,7 +301,9 @@ static void ArrangeLineHorizontal(
lineChild.Arrange(new Rect(currentLineOffsetX + lineChildOffset, currentLineOffsetY, lineChildWidth, currentLineSize));

lineChildOffset += lineChildWidth;
lineChildOffset += spacing;

if (lineChild.Visibility != Visibility.Collapsed)
lineChildOffset += spacing;
}
}

Expand All @@ -320,7 +330,9 @@ static void ArrangeLineVertical(
lineChild.Arrange(new Rect(currentLineOffsetX, currentLineOffsetY + lineChildOffset, currentLineSize, lineChildHeight));

lineChildOffset += lineChildHeight;
lineChildOffset += spacing;

if (lineChild.Visibility != Visibility.Collapsed)
lineChildOffset += spacing;
}
}
}
Expand Down
17 changes: 13 additions & 4 deletions WpfTest/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,23 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfTest"
xmlns:ws="https://github.com/OrgEleCho/EleCho.WpfSuite"
xmlns:b="http://schemas.microsoft.com/xaml/behaviors"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:hc="https://handyorg.github.io/handycontrol"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800"
Title="MainWindow" Height="550" Width="800"
Loaded="Window_Loaded"
d:DataContext="{d:DesignInstance Type=local:MainWindow}">

<Window.Resources>

</Window.Resources>

<DockPanel>
<ListBox DockPanel.Dock="Left"
Width="200"
<ListBox Name="AppNavigations"
DockPanel.Dock="Left"
Width="180"
BorderThickness="0 0 1 0"
ItemsSource="{Binding NavigationItems}"
SelectionChanged="ListBox_SelectionChanged">
<ListBox.ItemTemplate>
Expand All @@ -31,6 +35,11 @@
</ListBox.ItemTemplate>
</ListBox>
<Frame Name="AppFrame"
NavigationUIVisibility="Hidden"/>
NavigationUIVisibility="Hidden"
Navigated="AppFrame_Navigated">
<Frame.RenderTransform>
<TranslateTransform/>
</Frame.RenderTransform>
</Frame>
</DockPanel>
</Window>
30 changes: 26 additions & 4 deletions WpfTest/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
Expand Down Expand Up @@ -41,18 +42,23 @@ public MainWindow()
},
new NavigationItem()
{
Title = "Temp test",
Title = "TextBox test",
Description = "",
PageType = typeof(TempPage)
PageType = typeof(TextBoxTestPage)
},
new NavigationItem()
{
Title = "TextBox test",
Title = "Temp test",
Description = "",
PageType = typeof(TextBoxTestPage)
PageType = typeof(TempPage)
},
};

private void Window_Loaded(object sender, RoutedEventArgs e)
{
AppNavigations.SelectedItem = NavigationItems.FirstOrDefault();
}

private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (sender is not ListBox listBox ||
Expand All @@ -62,5 +68,21 @@ private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e
var page = Activator.CreateInstance(navigationItem.PageType);
AppFrame.Navigate(page);
}

private void AppFrame_Navigated(object sender, NavigationEventArgs e)
{
DoubleAnimation doubleAnimation = new DoubleAnimation()
{
From = 10,
To = 0,
Duration = new Duration(TimeSpan.FromMilliseconds(200)),
EasingFunction = new CircleEase()
{
EasingMode = EasingMode.EaseOut
}
};

AppFrame.RenderTransform.BeginAnimation(TranslateTransform.YProperty, doubleAnimation);
}
}
}
104 changes: 90 additions & 14 deletions WpfTest/Tests/CollectionTestPage.xaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<Page x:Class="WpfTest.Tests.CollectionTestPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfTest.Tests"
xmlns:ws="https://github.com/OrgEleCho/EleCho.WpfSuite"
mc:Ignorable="d"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
d:Background="White"
d:DataContext="{d:DesignInstance Type=local:CollectionTestPage}"
Title="CollectionTest">

Expand All @@ -17,17 +18,92 @@
</ws:ValueConverterGroup>
</Page.Resources>

<Grid>
<StackPanel>
<TextBlock Text="Has item"
Visibility="{Binding StringArray,Converter={StaticResource CollectionIsNotEmptyToVisibilityConverter}}"/>
<ws:ScrollViewer ScrollWithWheelDelta="True">
<Grid Margin="28 12 28 28">
<ws:StackPanel Spacing="12">
<TextBlock Text="Collection test"
FontSize="26"
Margin="0 0 0 12"/>

<ws:StackPanel Orientation="Horizontal"
HorizontalAlignment="Left"
Spacing="8">
<Button Content="Add item" Command="{Binding AddItemCommand}"/>
<Button Content="Remove item" Command="{Binding RemoveItemCommand}"/>
<ws:StackPanel Orientation="Horizontal"
HorizontalAlignment="Left"
Spacing="8">
<Button Content="Add item" Command="{Binding AddItemCommand}"/>
<Button Content="Remove item" Command="{Binding RemoveItemCommand}"/>
</ws:StackPanel>

<GroupBox Header="Items (WrapPanel)"
Padding="8"
Visibility="{Binding StringArray,Converter={StaticResource CollectionIsNotEmptyToVisibilityConverter}}">
<ItemsControl ItemsSource="{Binding StringArray}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<ws:WrapPanel HorizontalSpacing="4"
VerticalSpacing="4"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border BorderBrush="Gray"
BorderThickness="1"
CornerRadius="3"
Padding="5 3">
<ContentPresenter Content="{Binding}"/>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</GroupBox>

<GroupBox Header="Items (FlexPanel)"
Padding="8"
Visibility="{Binding StringArray,Converter={StaticResource CollectionIsNotEmptyToVisibilityConverter}}">
<ItemsControl ItemsSource="{Binding StringArray}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<ws:FlexPanel MainAlignment="SpaceBetween"
MainSpacing="4"
CrossSpacing="4"
UniformGrow="1"
Wrap="Wrap"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border BorderBrush="Gray"
BorderThickness="1"
CornerRadius="3"
Padding="5 3">
<ContentPresenter Content="{Binding}"/>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</GroupBox>

<GroupBox Header="Items (StackPanel)"
Padding="8"
Visibility="{Binding StringArray,Converter={StaticResource CollectionIsNotEmptyToVisibilityConverter}}">
<ItemsControl ItemsSource="{Binding StringArray}"
HorizontalAlignment="Left">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<ws:StackPanel Spacing="4"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border BorderBrush="Gray"
BorderThickness="1"
CornerRadius="3"
Padding="5 3">
<ContentPresenter Content="{Binding}"/>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</GroupBox>
</ws:StackPanel>
</StackPanel>
</Grid>
</Grid>
</ws:ScrollViewer>
</Page>
2 changes: 1 addition & 1 deletion WpfTest/Tests/TempPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<ws:StackPanel Spacing="8">
<ws:TextBox Width="200"
Placeholder="This is a text box with placeholder"/>
<ws:Image Source="/Assets/TestAvatar.jpg"
<ws:Image Source="/Assets/TestImage1.jpg"
CornerRadius="10"/>

<StackPanel>
Expand Down
1 change: 1 addition & 0 deletions WpfTest/WpfTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<ItemGroup>
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
<PackageReference Include="HandyControl" Version="3.5.1" />
<PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.77" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 0d1ad4c

Please sign in to comment.