using System; using System.Collections.Generic; using System.Linq; using System.Windows.Media; using System.Windows; using MultiTerm.Core.Types; namespace MultiTerm.Wpf.CustomControl; internal class Format { private readonly string? backgroundColorResourceName; public string Name { get; set; } public Brush BackgroundBrush { get; set; } public FormatType AssociatedFormatType { get; set; } public Format(string name, Brush backgroundBrush, FormatType associatedFormatType) { this.Name = name; this.BackgroundBrush = backgroundBrush; this.AssociatedFormatType = associatedFormatType; } public Format(string name, string backgroundColorResourceName, FormatType associatedFormatType) { this.Name = name; this.backgroundColorResourceName = backgroundColorResourceName; this.BackgroundBrush = Brushes.White; // set background brush to white this.AssociatedFormatType = associatedFormatType; } public static List GetListOfNames(IEnumerable formats) { return formats.Select(item => item.Name).ToList(); } public static void UpdateBackgroundBrushesFromResources(FrameworkElement fwElement, IEnumerable formats) { if (fwElement == null) throw new ArgumentNullException(nameof(fwElement)); foreach (var format in formats) { // if resource name not set => skip if (format.backgroundColorResourceName == null) continue; // get background brush color from resources try { format.BackgroundBrush = (SolidColorBrush)fwElement.FindResource(format.backgroundColorResourceName); } catch (Exception) { continue; } // ignore } } }