feat: make sure not all children are removed.

This commit is contained in:
rabbitism
2024-03-16 23:39:56 +08:00
parent f0740ff9a6
commit c0ea19c78c
2 changed files with 40 additions and 19 deletions

View File

@@ -26,6 +26,11 @@
Orientation="{Binding #Orientation.Value}"> Orientation="{Binding #Orientation.Value}">
<Button u:ToolBar.OverflowMode="Never" Content="Button 1" /> <Button u:ToolBar.OverflowMode="Never" Content="Button 1" />
<u:ToolBarSeparator /> <u:ToolBarSeparator />
<TextBox Width="100" VerticalAlignment="Center"></TextBox>
<StackPanel Orientation="Horizontal">
<RadioButton Content="A" VerticalAlignment="Center"></RadioButton>
<RadioButton Content="B" VerticalAlignment="Center"></RadioButton>
</StackPanel>
<Button u:ToolBar.OverflowMode="AsNeeded" Content="Button 2" /> <Button u:ToolBar.OverflowMode="AsNeeded" Content="Button 2" />
<Button u:ToolBar.OverflowMode="AsNeeded" Content="Button 3" /> <Button u:ToolBar.OverflowMode="AsNeeded" Content="Button 3" />
<ToggleButton Content="Toggle" /> <ToggleButton Content="Toggle" />

View File

@@ -100,37 +100,53 @@ public class ToolBarPanel: StackPanel
protected override Size ArrangeOverride(Size finalSize) protected override Size ArrangeOverride(Size finalSize)
{ {
Children.Clear(); //Children.Clear();
OverflowPanel?.Children.Clear(); //OverflowPanel?.Children.Clear();
var logicalChildren = _parent?.GetLogicalChildren().OfType<Control>().ToList(); var logicalChildren = _parent?.GetLogicalChildren().OfType<Control>().ToList();
List<Control> thisPanel = new List<Control>();
List<Control> thatPanel = new List<Control>();
if(logicalChildren is null) return finalSize; if(logicalChildren is null) return finalSize;
bool overflow = false; bool overflow = false;
foreach (var child in logicalChildren) for (int i = 0; i < logicalChildren.Count; i++)
{ {
if (ToolBar.GetIsOverflowItem(child)) var child = logicalChildren[i];
var isItemOverflow = ToolBar.GetIsOverflowItem(child);
if(isItemOverflow) overflow = true;
if (Children?.Contains(child) == true)
{ {
OverflowPanel?.Children.Add(child); if (isItemOverflow)
overflow = true; {
Children.Remove(child);
var overflowIndex = -1;
if (i > 1)
{
var last = logicalChildren[i - 1];
overflowIndex = OverflowPanel?.Children?.IndexOf(last) ?? -1;
}
OverflowPanel?.Children?.Insert(overflowIndex + 1, child);
}
} }
else else if (OverflowPanel?.Children?.Contains(child) == true)
{ {
Children.Add(child); if (!isItemOverflow)
} {
OverflowPanel.Children.Remove(child);
if (child is ToolBarSeparator s) var index = -1;
{ if (i > 1)
s.IsVisible = true; {
var last = logicalChildren[i - 1];
index = Children?.IndexOf(last) ?? -1;
}
Children?.Insert(index + 1, child);
}
} }
} }
var thisLast = this.Children.LastOrDefault(); if (this.Children?.LastOrDefault() is ToolBarSeparator s2)
if (thisLast is ToolBarSeparator s2)
{ {
s2.IsVisible = false; s2.IsVisible = false;
} }
if (OverflowPanel?.Children?.FirstOrDefault() is ToolBarSeparator s3)
var thatFirst = OverflowPanel?.Children.FirstOrDefault();
if (thatFirst is ToolBarSeparator s3)
{ {
s3.IsVisible = false; s3.IsVisible = false;
} }