namespace WPFluent.Markup; /// /// Custom design time attributes based on Marcin Najder implementation. /// /// /// /// <ui:FluentWindow /// xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml" /// /// ui:Design.Background="{DynamicResource ApplicationBackgroundBrush}" /// ui:Design.Foreground="{DynamicResource /// TextFillColorPrimaryBrush}"> /// <Button Content="Hello World" /> /// </FluentWindow> /// /// public static class Design { private static bool? _inDesignMode; private static readonly string[] DesignProcesses = [ "devenv", "dotnet", "RiderWpfPreviewerLauncher64" ]; 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)); 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); } /// /// 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)); /// /// 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 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. /// Background property value. public static void SetBackground(DependencyObject dependencyObject, System.Windows.Media.Brush? value) => dependencyObject.SetValue( BackgroundProperty, value); /// /// 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); }