Browse Source

improve UpdateFlowDirection

daniel mayost 3 years ago
parent
commit
402790ba86

+ 22 - 6
src/Avalonia.Controls/ComboBox.cs

@@ -178,10 +178,16 @@ namespace Avalonia.Controls
                 ComboBoxItem.ContentTemplateProperty);
         }
 
+        protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
+        {
+            base.OnAttachedToVisualTree(e);
+            UpdateSelectionBoxItem(SelectedItem);
+        }
+
         public override void InvalidateMirrorTransform()
         {
             base.InvalidateMirrorTransform();
-            UpdateSelectionBoxItem(SelectedItem);
+            UpdateFlowDirection();
         }
 
         /// <inheritdoc/>
@@ -347,7 +353,7 @@ namespace Avalonia.Controls
                 parent.GetObservable(IsVisibleProperty).Subscribe(IsVisibleChanged).DisposeWith(_subscriptionsOnOpen);
             }
 
-            UpdateSelectionBoxItem(SelectedItem);
+            UpdateFlowDirection();
         }
 
         private void IsVisibleChanged(bool isVisible)
@@ -403,12 +409,8 @@ namespace Avalonia.Controls
                 {
                     control.Measure(Size.Infinity);
 
-                    var flowDirection = 
-                        (control.VisualParent as Control)?.FlowDirection ?? FlowDirection.LeftToRight;
-
                     SelectionBoxItem = new Rectangle
                     {
-                        FlowDirection = flowDirection,
                         Width = control.DesiredSize.Width,
                         Height = control.DesiredSize.Height,
                         Fill = new VisualBrush
@@ -419,6 +421,8 @@ namespace Avalonia.Controls
                         }
                     };
                 }
+
+                UpdateFlowDirection();
             }
             else
             {
@@ -426,6 +430,18 @@ namespace Avalonia.Controls
             }
         }
 
+        private void UpdateFlowDirection()
+        {
+            var rectangle = SelectionBoxItem as Rectangle;
+            if (rectangle != null)
+            {
+                var content = (rectangle.Fill as VisualBrush)!.Visual as Control;
+                var flowDirection = (((IVisual)content!).VisualParent as Control)?.FlowDirection ?? FlowDirection.LeftToRight;
+
+                rectangle.FlowDirection = flowDirection;
+            }
+        }
+
         private void SelectFocusedItem()
         {
             foreach (ItemContainerInfo dropdownItem in ItemContainerGenerator.Containers)

+ 3 - 2
tests/Avalonia.Controls.UnitTests/ComboBoxTests.cs

@@ -351,6 +351,7 @@ namespace Avalonia.Controls.UnitTests
                 };
                 var target = new ComboBox
                 {
+                    FlowDirection = FlowDirection.RightToLeft,
                     Items = items,
                     Template = GetTemplate()
                 };
@@ -368,7 +369,7 @@ namespace Avalonia.Controls.UnitTests
         [Fact]
         public void FlowDirection_Of_RectangleContent_Updated_After_Change_ComboBox()
         {
-            using (UnitTestApplication.Start(TestServices.StyledWindow))
+            using (UnitTestApplication.Start(TestServices.RealStyler))
             {
                 var items = new[]
                 {
@@ -385,10 +386,10 @@ namespace Avalonia.Controls.UnitTests
                 };
 
                 var root = new TestRoot(target);
-                
                 target.ApplyTemplate();
                 target.Presenter.ApplyTemplate();
                 target.SelectedIndex = 0;
+                ((ContentPresenter)target.Presenter).UpdateChild();
 
                 var rectangle = target.GetValue(ComboBox.SelectionBoxItemProperty) as Rectangle;