diff --git a/HandyControl/Controls/Carousel.cs b/HandyControl/Controls/Carousel.cs index 6715a2ab..f25c2f45 100644 --- a/HandyControl/Controls/Carousel.cs +++ b/HandyControl/Controls/Carousel.cs @@ -40,6 +40,14 @@ namespace HandyControl.Controls private ItemsPresenter _itemsControl; + private int _pageIndex = -1; + + private Button _selectedButton; + + private DispatcherTimer _updateTimer; + + private readonly List _widthList = new List(); + #endregion Data public override void OnApplyTemplate() @@ -63,7 +71,7 @@ namespace HandyControl.Controls private void Update() { TimerSwitch(AutoRun); - UpdatePageButtons(); + UpdatePageButtons(_pageIndex); } private void CheckNull() @@ -99,14 +107,6 @@ namespace HandyControl.Controls set => SetValue(IsCenterProperty, value); } - private int _pageIndex; - - private Button _selectedButton; - - private DispatcherTimer _updateTimer; - - private readonly List _widthList = new List(); - public Carousel() { CommandBindings.Add(new CommandBinding(ControlCommands.Prev, ButtonPrev_OnClick)); @@ -167,24 +167,21 @@ namespace HandyControl.Controls private void TimerSwitch(bool run) { if (!_appliedTemplate) return; - if (run) + + if (_updateTimer != null) { - _updateTimer = new DispatcherTimer - { - Interval = Interval - }; - _updateTimer.Tick += UpdateTimer_Tick; - _updateTimer.Start(); + _updateTimer.Tick -= UpdateTimer_Tick; + _updateTimer.Stop(); + _updateTimer = null; } - else + + if (!run) return; + _updateTimer = new DispatcherTimer { - if (_updateTimer != null) - { - _updateTimer.Tick -= UpdateTimer_Tick; - _updateTimer.Stop(); - _updateTimer = null; - } - } + Interval = Interval + }; + _updateTimer.Tick += UpdateTimer_Tick; + _updateTimer.Start(); } private void UpdateTimer_Tick(object sender, EventArgs e) @@ -196,40 +193,41 @@ namespace HandyControl.Controls /// /// 更新页按钮 /// - public void UpdatePageButtons(int? index = null) + public void UpdatePageButtons(int index = -1) { if (!_appliedTemplate) return; - if (index == null) - { - var count = Items.Count; - _widthList.Clear(); - _widthList.Add(0); - var width = .0; - foreach (FrameworkElement item in Items) - { - item.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity)); - width += item.DesiredSize.Width; - _widthList.Add(width); - } - _itemsControl.Width = _widthList.Last() + ExtendWidth; - _panelPage.Children.Clear(); - for (var i = 0; i < count; i++) - _panelPage.Children.Add(CreatePateButton()); + var count = Items.Count; + _widthList.Clear(); + _widthList.Add(0); + var width = .0; + foreach (FrameworkElement item in Items) + { + item.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity)); + width += item.DesiredSize.Width; + _widthList.Add(width); + } + + _itemsControl.Width = _widthList.Last() + ExtendWidth; + _panelPage.Children.Clear(); + for (var i = 0; i < count; i++) + { + _panelPage.Children.Add(CreatePateButton()); + } + + if (index == -1) + { if (count > 0) { var button = _panelPage.Children[0]; button.RaiseEvent(new RoutedEventArgs(ButtonBase.ClickEvent, button)); } } - else + else if (index >= 0 && index < count) { - if (index >= 0 && index < _panelPage.Children.Count) - { - var button = _panelPage.Children[(int)index]; - button.RaiseEvent(new RoutedEventArgs(ButtonBase.ClickEvent, button)); - UpdateItemsPosition(); - } + var button = _panelPage.Children[index]; + button.RaiseEvent(new RoutedEventArgs(ButtonBase.ClickEvent, button)); + UpdateItemsPosition(); } } diff --git a/HandyControl/Controls/Clock/Clock.cs b/HandyControl/Controls/Clock/Clock.cs index cf7707bb..9447c58b 100644 --- a/HandyControl/Controls/Clock/Clock.cs +++ b/HandyControl/Controls/Clock/Clock.cs @@ -241,14 +241,7 @@ namespace HandyControl.Controls _appliedTemplate = true; if (_isLoaded) { - if (SelectedTime.HasValue) - { - Update(SelectedTime.Value); - } - else - { - DisplayTime = DateTime.Now; - } + Update(SelectedTime ?? DisplayTime); } } diff --git a/HandyControl/Controls/ColorPicker.cs b/HandyControl/Controls/ColorPicker.cs index 51a426ec..a41544cc 100644 --- a/HandyControl/Controls/ColorPicker.cs +++ b/HandyControl/Controls/ColorPicker.cs @@ -51,6 +51,8 @@ namespace HandyControl.Controls private Slider _sliderOpacity; + private bool _appliedTemplate; + /// /// 当前显示的颜色类型 /// @@ -348,6 +350,7 @@ namespace HandyControl.Controls public override void OnApplyTemplate() { + _appliedTemplate = false; if (_sliderColor != null) { _sliderColor.ValueChanged -= SliderColor_OnValueChanged; @@ -392,6 +395,7 @@ namespace HandyControl.Controls collection.Add(behavior); } + _appliedTemplate = true; if (_isLoaded) { Init(); @@ -405,6 +409,7 @@ namespace HandyControl.Controls private void Init() { UpdateStatus(SelectedBrush.Color); + _panelColor.Children.Clear(); foreach (var item in _colorPresetList) { _panelColor.Children.Add(CreateColorButton(item)); @@ -558,7 +563,7 @@ namespace HandyControl.Controls private void SliderColor_OnValueChanged(object sender, RoutedPropertyChangedEventArgs e) { - if (!_isLoaded || !IsNeedUpdateInfo) return; + if (!_appliedTemplate || !IsNeedUpdateInfo) return; var index = Math.Min(5, (int)Math.Floor(e.NewValue)); var sub = e.NewValue - index; var range = _colorRangeList[index]; @@ -573,7 +578,7 @@ namespace HandyControl.Controls private void SliderOpacity_OnValueChanged(object sender, RoutedPropertyChangedEventArgs e) { - if (!_isLoaded || !IsNeedUpdateInfo) return; + if (!_appliedTemplate || !IsNeedUpdateInfo) return; var color = SelectedBrush.Color; SelectedBrush = new SolidColorBrush(Color.FromArgb((byte)_sliderOpacity.Value, color.R, color.G, color.B)); } @@ -638,7 +643,7 @@ namespace HandyControl.Controls // ReSharper disable once UnusedParameter.Local private void NumericUpDownRgb_OnValueChanged(object sender, FunctionEventArgs e) { - if (!_isLoaded || !IsNeedUpdateInfo) return; + if (!_appliedTemplate || !IsNeedUpdateInfo) return; if (e.OriginalSource is NumericUpDown ctl && ctl.Tag is string tag) { var color = SelectedBrush.Color; diff --git a/HandyControl/Controls/Slider/CompareSlider.cs b/HandyControl/Controls/Slider/CompareSlider.cs index d3d588d2..347a3790 100644 --- a/HandyControl/Controls/Slider/CompareSlider.cs +++ b/HandyControl/Controls/Slider/CompareSlider.cs @@ -24,25 +24,36 @@ namespace HandyControl.Controls set => SetValue(SourceContentProperty, value); } - private bool _isLoaded; + //private bool _isLoaded; - private double _tempValue; + //private double _tempValue; - public CompareSlider() - { - Loaded += delegate - { - if (_isLoaded) return; - _isLoaded = true; - Value = _tempValue; - }; - } + //public CompareSlider() + //{ + // //Loaded += delegate + // //{ + // // if (_isLoaded) return; + // // _isLoaded = true; + // // Value = _tempValue; + // //}; + //} - public override void OnApplyTemplate() - { - base.OnApplyTemplate(); - _tempValue = Value; - Value = Orientation == Orientation.Horizontal ? Maximum : 0; - } + ////protected override void OnRender(DrawingContext drawingContext) + ////{ + //// base.OnRender(drawingContext); + + //// Value = _tempValue; + ////} + + ////public override void OnApplyTemplate() + ////{ + //// base.OnApplyTemplate(); + //// _tempValue = Value; + //// Value = Orientation == Orientation.Horizontal ? Maximum : 0; + //// if (_isLoaded) + //// { + //// Value = _tempValue; + //// } + ////} } } \ No newline at end of file diff --git a/HandyControl/Controls/Slider/CompareTrack.cs b/HandyControl/Controls/Slider/CompareTrack.cs new file mode 100644 index 00000000..ec98f390 --- /dev/null +++ b/HandyControl/Controls/Slider/CompareTrack.cs @@ -0,0 +1,117 @@ +using System; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Controls.Primitives; + +namespace HandyControl.Controls +{ + public class CompareTrack : Track + { + protected override Size ArrangeOverride(Size arrangeSize) + { + base.ArrangeOverride(arrangeSize); + + var isVertical = Orientation == Orientation.Vertical; + ComputeSliderLengths(arrangeSize, isVertical, out var decreaseButtonLength, out var thumbLength, + out var increaseButtonLength); + + var offset = new Point(); + var pieceSize = arrangeSize; + var isDirectionReversed = IsDirectionReversed; + + if (isVertical) + { + CoerceLength(ref decreaseButtonLength, arrangeSize.Height); + CoerceLength(ref increaseButtonLength, arrangeSize.Height); + CoerceLength(ref thumbLength, arrangeSize.Height); + + offset.Y = isDirectionReversed ? decreaseButtonLength + thumbLength : 0.0; + pieceSize.Height = increaseButtonLength; + + IncreaseRepeatButton?.Arrange(new Rect(offset, pieceSize)); + + offset.Y = isDirectionReversed ? 0.0 : increaseButtonLength + thumbLength; + pieceSize.Height = decreaseButtonLength; + + if (DecreaseRepeatButton != null) + { + DecreaseRepeatButton.Arrange(new Rect(offset, pieceSize)); + DecreaseRepeatButton.Height = pieceSize.Height; + } + + offset.Y = isDirectionReversed ? decreaseButtonLength : increaseButtonLength; + pieceSize.Height = thumbLength; + + Thumb?.Arrange(new Rect(offset, pieceSize)); + } + else + { + CoerceLength(ref decreaseButtonLength, arrangeSize.Width); + CoerceLength(ref increaseButtonLength, arrangeSize.Width); + CoerceLength(ref thumbLength, arrangeSize.Width); + + offset.X = isDirectionReversed ? increaseButtonLength + thumbLength : 0.0; + pieceSize.Width = decreaseButtonLength; + + DecreaseRepeatButton?.Arrange(new Rect(offset, pieceSize)); + + + offset.X = isDirectionReversed ? 0.0 : decreaseButtonLength + thumbLength; + pieceSize.Width = increaseButtonLength; + + if (IncreaseRepeatButton != null) + { + IncreaseRepeatButton.Arrange(new Rect(offset, pieceSize)); + IncreaseRepeatButton.Width = pieceSize.Width; + } + + offset.X = isDirectionReversed ? increaseButtonLength : decreaseButtonLength; + pieceSize.Width = thumbLength; + + Thumb?.Arrange(new Rect(offset, pieceSize)); + } + + return arrangeSize; + } + + private void ComputeSliderLengths(Size arrangeSize, bool isVertical, out double decreaseButtonLength, + out double thumbLength, out double increaseButtonLength) + { + var min = Minimum; + var range = Math.Max(0.0, Maximum - min); + var offset = Math.Min(range, Value - min); + + double trackLength; + + // Compute thumb size + if (isVertical) + { + trackLength = arrangeSize.Height; + thumbLength = Thumb?.DesiredSize.Height ?? 0; + } + else + { + trackLength = arrangeSize.Width; + thumbLength = Thumb?.DesiredSize.Width ?? 0; + } + + CoerceLength(ref thumbLength, trackLength); + + var remainingTrackLength = trackLength - thumbLength; + + decreaseButtonLength = remainingTrackLength * offset / range; + CoerceLength(ref decreaseButtonLength, remainingTrackLength); + + increaseButtonLength = remainingTrackLength - decreaseButtonLength; + CoerceLength(ref increaseButtonLength, remainingTrackLength); + } + + private static void CoerceLength(ref double componentLength, double trackLength) + { + if (componentLength < 0) + componentLength = 0.0; + else if (componentLength > trackLength || double.IsNaN(componentLength)) + componentLength = trackLength; + } + } +} \ No newline at end of file diff --git a/HandyControl/Controls/StepBar/StepBar.cs b/HandyControl/Controls/StepBar/StepBar.cs index cdfcde62..3b23b0a0 100644 --- a/HandyControl/Controls/StepBar/StepBar.cs +++ b/HandyControl/Controls/StepBar/StepBar.cs @@ -33,13 +33,10 @@ namespace HandyControl.Controls private void ItemContainerGenerator_StatusChanged(object sender, EventArgs e) { - var count = Items.Count; - if (count > 0) + if (ItemContainerGenerator.Status == GeneratorStatus.ContainersGenerated) { - if (ItemContainerGenerator.ContainerFromIndex(0) is StepBarItem item) - { - item.Status = StepStatus.UnderWay; - } + var count = Items.Count; + if (count <= 0) return; for (int i = 0; i < count; i++) { @@ -48,6 +45,21 @@ namespace HandyControl.Controls stepBarItem.Index = i + 1; } } + + if (StepIndex < count) + { + for (int i = 0; i < StepIndex; i++) + { + if (ItemContainerGenerator.ContainerFromIndex(i) is StepBarItem stepBarItem) + { + stepBarItem.Status = StepStatus.Complete; + } + } + if (ItemContainerGenerator.ContainerFromIndex(StepIndex) is StepBarItem item) + { + item.Status = StepStatus.UnderWay; + } + } } } @@ -77,7 +89,7 @@ namespace HandyControl.Controls public int StepIndex { - get => (int) GetValue(StepIndexProperty); + get => (int)GetValue(StepIndexProperty); protected set => SetValue(StepIndexProperty, value); } @@ -86,7 +98,7 @@ namespace HandyControl.Controls public Dock Dock { - get => (Dock) GetValue(DockProperty); + get => (Dock)GetValue(DockProperty); set => SetValue(DockProperty, value); } diff --git a/HandyControl/Controls/TabControl/TabItem.cs b/HandyControl/Controls/TabControl/TabItem.cs index f93e22b4..f25ff7b0 100644 --- a/HandyControl/Controls/TabControl/TabItem.cs +++ b/HandyControl/Controls/TabControl/TabItem.cs @@ -11,13 +11,8 @@ using HandyControl.Tools; // ReSharper disable once CheckNamespace namespace HandyControl.Controls { - [TemplatePart(Name = CloseButtonKey, Type = typeof(Button))] public class TabItem : System.Windows.Controls.TabItem { - private const string CloseButtonKey = "PART_CloseButton"; - - private Button _buttonClose; - /// /// 动画速度 /// @@ -170,16 +165,6 @@ namespace HandyControl.Controls Focus(); } - public override void OnApplyTemplate() - { - base.OnApplyTemplate(); - _buttonClose = Template.FindName(CloseButtonKey, this) as Button; - if (_buttonClose != null) - { - _buttonClose.Click += Close; - } - } - /// /// 关闭 /// diff --git a/HandyControl/HandyControl.csproj b/HandyControl/HandyControl.csproj index a480ea86..07572ee5 100644 --- a/HandyControl/HandyControl.csproj +++ b/HandyControl/HandyControl.csproj @@ -59,6 +59,7 @@ + diff --git a/HandyControl/Themes/Styles/Base/StepBarBaseStyle.xaml b/HandyControl/Themes/Styles/Base/StepBarBaseStyle.xaml index 06e48c1c..32b383fc 100644 --- a/HandyControl/Themes/Styles/Base/StepBarBaseStyle.xaml +++ b/HandyControl/Themes/Styles/Base/StepBarBaseStyle.xaml @@ -63,9 +63,7 @@ - - - + diff --git a/HandyControl/Themes/Styles/Slider.xaml b/HandyControl/Themes/Styles/Slider.xaml index c4fd9975..5d4f88e2 100644 --- a/HandyControl/Themes/Styles/Slider.xaml +++ b/HandyControl/Themes/Styles/Slider.xaml @@ -218,11 +218,11 @@ - + - + @@ -231,7 +231,7 @@ - + @@ -240,7 +240,7 @@ - + @@ -322,7 +322,7 @@ - + @@ -335,7 +335,7 @@ - + @@ -344,7 +344,7 @@ - + diff --git a/HandyControl/Themes/Styles/TabControl.xaml b/HandyControl/Themes/Styles/TabControl.xaml index bd1ad7a7..b81a0a96 100644 --- a/HandyControl/Themes/Styles/TabControl.xaml +++ b/HandyControl/Themes/Styles/TabControl.xaml @@ -55,7 +55,7 @@ -