private static void BackgroundPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (e.NewValue != e.OldValue) { if (d is FrameworkElement sender) { var NewBrush = e.NewValue as Brush; var OldBrush = e.OldValue as Brush; DependencyProperty BackgroundProperty = null; if (sender is Panel) { BackgroundProperty = Panel.BackgroundProperty; } else if (sender is Control) { BackgroundProperty = Control.BackgroundProperty; } else if (sender is Shape) { BackgroundProperty = Shape.FillProperty; } if (BackgroundProperty == null) { return; } if (sender.GetValue(BackgroundProperty) is IFluentBrush tmp_fluent) { sender.SetValue(BackgroundProperty, OldBrush); //tmp_fluent.ClearEventList(); //tmp_fluent.Dispose(); } IFluentBrush FluentBrush = null; if (OldBrush is SolidColorBrush && NewBrush is SolidColorBrush) { FluentBrush = new FluentSolidColorBrush() { Duration = GetDuration(sender), BaseBrush = OldBrush, }; } else if (OldBrush is LinearGradientBrush && NewBrush is SolidColorBrush || OldBrush is SolidColorBrush && NewBrush is LinearGradientBrush || OldBrush is LinearGradientBrush && NewBrush is LinearGradientBrush) { FluentBrush = new FluentCompositeBrush() { Duration = GetDuration(sender), BaseBrush = OldBrush, }; } if (FluentBrush == null) { sender.SetValue(BackgroundProperty, NewBrush); return; } FluentBrush.TransitionCompleted += (s, a) => { sender.SetValue(BackgroundProperty, a.NewBrush); //if (s is IFluentBrush tmp_brush) //{ // tmp_brush.ClearEventList(); // tmp_brush.Dispose(); //} }; sender.SetValue(BackgroundProperty, FluentBrush); FluentBrush.BaseBrush = NewBrush; if (NewBrush is SolidColorBrush tmp_new_brush) { //tmp_new_brush.RegisterPropertyChangedCallback(SolidColorBrush.ColorProperty,Col) } } } }
protected override void OnDetaching() { AssociatedObject?.SetValue(BackgroundProperty, NowBrush); FluentBrush = null; }