Browse Source

Set Track's logical child.

To its Thumb. This fixes a problem with styling ScrollBar.
Steven Kirk 9 năm trước cách đây
mục cha
commit
13599a6ebc

+ 22 - 20
src/Perspex.Controls/Primitives/Track.cs

@@ -3,7 +3,6 @@
 
 using System;
 using Perspex.Input;
-using Perspex.Interactivity;
 using Perspex.Metadata;
 
 namespace Perspex.Controls.Primitives
@@ -34,28 +33,10 @@ namespace Perspex.Controls.Primitives
 
         static Track()
         {
+            ThumbProperty.Changed.AddClassHandler<Track>(x => x.ThumbChanged);
             AffectsArrange(MinimumProperty, MaximumProperty, ValueProperty, OrientationProperty);
         }
 
-        public Track()
-        {
-            this.GetObservableWithHistory(ThumbProperty).Subscribe(val =>
-            {
-                if (val.Item1 != null)
-                {
-                    val.Item1.DragDelta -= ThumbDragged;
-                }
-
-                VisualChildren.Clear();
-
-                if (val.Item2 != null)
-                {
-                    val.Item2.DragDelta += ThumbDragged;
-                    VisualChildren.Add(val.Item2);
-                }
-            });
-        }
-
         public double Minimum
         {
             get { return _minimum; }
@@ -151,6 +132,27 @@ namespace Perspex.Controls.Primitives
             return finalSize;
         }
 
+        private void ThumbChanged(PerspexPropertyChangedEventArgs e)
+        {
+            var oldThumb = (Thumb)e.OldValue;
+            var newThumb = (Thumb)e.NewValue;
+
+            if (oldThumb != null)
+            {
+                oldThumb.DragDelta -= ThumbDragged;
+            }
+
+            LogicalChildren.Clear();
+            VisualChildren.Clear();
+
+            if (newThumb != null)
+            {
+                newThumb.DragDelta += ThumbDragged;
+                LogicalChildren.Add(newThumb);
+                VisualChildren.Add(newThumb);
+            }
+        }
+
         private void ThumbDragged(object sender, VectorEventArgs e)
         {
             double range = Maximum - Minimum;

+ 1 - 2
src/Perspex.Themes.Default/ScrollBar.paml

@@ -11,8 +11,7 @@
             <Thumb Name="thumb">
               <Thumb.Template>
                 <ControlTemplate>
-                  <!-- TODO: Find out why StyleResource doesn't work here -->
-                  <Border Background="Gray"/>
+                  <Border Background="{StyleResource ThemeControlDarkBrush}"/>
                 </ControlTemplate>
               </Thumb.Template>
             </Thumb>

+ 22 - 0
tests/Perspex.Controls.UnitTests/Primitives/TrackTests.cs

@@ -2,6 +2,7 @@
 // Licensed under the MIT license. See licence.md file in the project root for full license information.
 
 using Perspex.Controls.Primitives;
+using Perspex.LogicalTree;
 using Xunit;
 
 namespace Perspex.Controls.UnitTests.Primitives
@@ -118,5 +119,26 @@ namespace Perspex.Controls.UnitTests.Primitives
 
             Assert.Equal(new Rect(0, 0, 100, 12), thumb.Bounds);
         }
+
+        [Fact]
+        public void Thumb_Should_Be_Logical_Child()
+        {
+            var thumb = new Thumb
+            {
+                Height = 12,
+            };
+
+            var target = new Track
+            {
+                Height = 12,
+                Thumb = thumb,
+                Orientation = Orientation.Horizontal,
+                Minimum = 100,
+                Maximum = 100,
+            };
+
+            Assert.Same(thumb.Parent, target);
+            Assert.Equal(new[] { thumb }, ((ILogical)target).LogicalChildren);
+        }
     }
 }