using System.ComponentModel; using System.Windows; namespace WPFDark; public static class Design { private static readonly string[] DesignProcesses = [ "devenv", "dotnet", "RiderWpfPreviewerLauncher64" ]; private static bool? _inDesignMode; /// /// Gets a value indicating whether the framework is in design-time mode. (Caliburn.Micro implementation) /// private static bool InDesignMode => _inDesignMode ??= (bool) DependencyPropertyDescriptor .FromProperty(DesignerProperties.IsInDesignModeProperty, typeof(FrameworkElement)) .Metadata.DefaultValue || DesignProcesses.Any(process => System .Diagnostics.Process.GetCurrentProcess() .ProcessName.StartsWith(process, StringComparison.Ordinal)); public static readonly DependencyProperty BackgroundProperty = DependencyProperty.RegisterAttached( "Background", typeof(System.Windows.Media.Brush), typeof(Design), new PropertyMetadata(OnBackgroundChanged) ); public static readonly DependencyProperty ForegroundProperty = DependencyProperty.RegisterAttached( "Foreground", typeof(System.Windows.Media.Brush), typeof(Design), new PropertyMetadata(OnForegroundChanged) ); /// Helper for getting from . /// to read from. /// Background property value. [System.Diagnostics.CodeAnalysis.SuppressMessage( "WpfAnalyzers.DependencyProperty", "WPF0033:Add [AttachedPropertyBrowsableForType]", Justification = "Because" )] public static System.Windows.Media.Brush? GetBackground(DependencyObject dependencyObject) => (System.Windows.Media.Brush)dependencyObject.GetValue(BackgroundProperty); /// Helper for setting on . /// to set on. /// Background property value. public static void SetBackground(DependencyObject dependencyObject, System.Windows.Media.Brush? value) => dependencyObject.SetValue(BackgroundProperty, value); /// Helper for getting from . /// to read from. /// Foreground property value. [System.Diagnostics.CodeAnalysis.SuppressMessage( "WpfAnalyzers.DependencyProperty", "WPF0033:Add [AttachedPropertyBrowsableForType]", Justification = "Because" )] public static System.Windows.Media.Brush? GetForeground(DependencyObject dependencyObject) => (System.Windows.Media.Brush)dependencyObject.GetValue(ForegroundProperty); /// Helper for setting on . /// to set on. /// Foreground property value. public static void SetForeground(DependencyObject dependencyObject, System.Windows.Media.Brush? value) => dependencyObject.SetValue(ForegroundProperty, value); private static void OnBackgroundChanged(DependencyObject? d, DependencyPropertyChangedEventArgs e) { if (!InDesignMode) { return; } d?.GetType()?.GetProperty("Background")?.SetValue(d, e.NewValue, null); } private static void OnForegroundChanged(DependencyObject? d, DependencyPropertyChangedEventArgs e) { if (!InDesignMode) { return; } d?.GetType()?.GetProperty("Foreground")?.SetValue(d, e.NewValue, null); } }