using System.ComponentModel; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Media; namespace WPFluent.Controls.Primitives; /// /// ContentPresenter is used within the template of a content control to denote the /// place in the control's visual tree (control template) where the content /// is to be added. /// public class ContentPresenterEx : ContentPresenter { /// /// DependencyProperty for property. /// public static readonly DependencyProperty FontFamilyProperty = TextElement.FontFamilyProperty.AddOwner(typeof(ContentPresenterEx)); /// /// The FontFamily property specifies the name of font family. /// [Localizability(LocalizationCategory.Font)] public FontFamily FontFamily { get => (FontFamily)GetValue(FontFamilyProperty); set => SetValue(FontFamilyProperty, value); } /// /// DependencyProperty for property. /// public static readonly DependencyProperty FontStyleProperty = TextElement.FontStyleProperty.AddOwner(typeof(ContentPresenterEx)); /// /// The FontStyle property requests normal, italic, and oblique faces within a font family. /// public FontStyle FontStyle { get => (FontStyle)GetValue(FontStyleProperty); set => SetValue(FontStyleProperty, value); } /// /// DependencyProperty for property. /// public static readonly DependencyProperty FontWeightProperty = TextElement.FontWeightProperty.AddOwner(typeof(ContentPresenterEx)); /// /// The FontWeight property specifies the weight of the font. /// public FontWeight FontWeight { get => (FontWeight)GetValue(FontWeightProperty); set => SetValue(FontWeightProperty, value); } /// /// DependencyProperty for property. /// public static readonly DependencyProperty FontStretchProperty = TextElement.FontStretchProperty.AddOwner(typeof(ContentPresenterEx)); /// /// The FontStretch property selects a normal, condensed, or extended face from a font family. /// public FontStretch FontStretch { get => (FontStretch)GetValue(FontStretchProperty); set => SetValue(FontStretchProperty, value); } /// /// DependencyProperty for property. /// public static readonly DependencyProperty FontSizeProperty = TextElement.FontSizeProperty.AddOwner(typeof(ContentPresenterEx)); /// /// The FontSize property specifies the size of the font. /// [TypeConverter(typeof(FontSizeConverter))] [Localizability(LocalizationCategory.None)] public double FontSize { get => (double)GetValue(FontSizeProperty); set => SetValue(FontSizeProperty, value); } /// /// DependencyProperty for property. /// public static readonly DependencyProperty ForegroundProperty = TextElement.ForegroundProperty.AddOwner(typeof(ContentPresenterEx)); /// /// The Foreground property specifies the foreground brush of an element's text content. /// public Brush Foreground { get => (Brush)GetValue(ForegroundProperty); set => SetValue(ForegroundProperty, value); } /// /// DependencyProperty for property. /// public static readonly DependencyProperty LineHeightProperty = Block.LineHeightProperty.AddOwner(typeof(ContentPresenterEx)); /// /// The LineHeight property specifies the height of each generated line box. /// [TypeConverter(typeof(LengthConverter))] public double LineHeight { get => (double)GetValue(LineHeightProperty); set => SetValue(LineHeightProperty, value); } /// /// DependencyProperty for property. /// public static readonly DependencyProperty LineStackingStrategyProperty = Block.LineStackingStrategyProperty.AddOwner(typeof(ContentPresenterEx)); /// /// The LineStackingStrategy property specifies how lines are placed /// public LineStackingStrategy LineStackingStrategy { get => (LineStackingStrategy)GetValue(LineStackingStrategyProperty); set => SetValue(LineStackingStrategyProperty, value); } /// /// DependencyProperty for property. /// public static readonly DependencyProperty TextWrappingProperty = System.Windows.Controls.TextBlock.TextWrappingProperty.AddOwner(typeof(ContentPresenterEx), new FrameworkPropertyMetadata(TextWrapping.NoWrap, new PropertyChangedCallback(OnTextWrappingChanged))); /// /// The TextWrapping property controls whether or not text wraps /// when it reaches the flow edge of its containing block box. /// public TextWrapping TextWrapping { get => (TextWrapping)GetValue(TextWrappingProperty); set => SetValue(TextWrappingProperty, value); } private static void OnTextWrappingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var ctrl = (ContentPresenterEx)d; if (ctrl.TextBlock != null) { ctrl.TextBlock.TextWrapping = (TextWrapping)e.NewValue; } else if (ctrl.AccessText != null) { ctrl.AccessText.TextWrapping = (TextWrapping)e.NewValue; } } private bool IsUsingDefaultTemplate { get; set; } private TextBlock _textBlock = null!; private TextBlock TextBlock { get => _textBlock; set { if (_textBlock != null) { _textBlock.ClearValue(System.Windows.Controls.TextBlock.TextWrappingProperty); } _textBlock = value; if (_textBlock != null) { _textBlock.TextWrapping = TextWrapping; } } } private AccessText _accessText = null!; private AccessText AccessText { get => _accessText; set { if (_accessText != null) { _accessText.ClearValue(AccessText.TextWrappingProperty); } _accessText = value; if (_accessText != null) { _accessText.TextWrapping = TextWrapping; } } } protected override DataTemplate ChooseTemplate() { DataTemplate template = null!; var content = Content; // ContentTemplate has first stab template = ContentTemplate; // no ContentTemplate set, try ContentTemplateSelector if (template == null) { if (ContentTemplateSelector != null) { template = ContentTemplateSelector.SelectTemplate(content, this); } } // if that failed, try the default TemplateSelector if (template == null) { template = base.ChooseTemplate(); IsUsingDefaultTemplate = true; } else { IsUsingDefaultTemplate = false; } return template; } protected override void OnVisualChildrenChanged(DependencyObject visualAdded, DependencyObject visualRemoved) { base.OnVisualChildrenChanged(visualAdded, visualRemoved); if (visualAdded != null && IsUsingDefaultTemplate) { if (visualAdded is TextBlock textBlock) { TextBlock = textBlock; } else if (visualAdded is AccessText accessText) { AccessText = accessText; } } else if (visualRemoved != null) { if (visualRemoved == TextBlock) { TextBlock = null!; } else if (visualRemoved == AccessText) { AccessText = null!; } } } }