Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is what I did. Created a MarkupExtension that converts Font size to EM based on font assigned on Window.</p> <p>I would like to thank <a href="http://10rem.net/blog/2011/03/09/creating-a-custom-markup-extension-in-wpf-and-soon-silverlight" rel="noreferrer">http://10rem.net/blog/2011/03/09/creating-a-custom-markup-extension-in-wpf-and-soon-silverlight</a></p> <p>and </p> <p><a href="http://tomlev2.wordpress.com/tag/markup-extension/" rel="noreferrer">http://tomlev2.wordpress.com/tag/markup-extension/</a></p> <p>for providing required knowledge.</p> <pre><code>[MarkupExtensionReturnType(typeof(double))] public class EmFontSize : MarkupExtension { public EmFontSize() { } public EmFontSize(double size) { Size = size; } [ConstructorArgument("size")] public double Size { get; set; } public override object ProvideValue(IServiceProvider serviceProvider) { if (serviceProvider == null) return null; // get the target of the extension from the IServiceProvider interface IProvideValueTarget ipvt = (IProvideValueTarget)serviceProvider.GetService(typeof(IProvideValueTarget)); if (ipvt.TargetObject.GetType().FullName == "System.Windows.SharedDp") return this; DependencyObject targetObject = ipvt.TargetObject as DependencyObject; var window = TryFindParent&lt;Window&gt;(targetObject); if (window != null) { return window.FontSize * Size; } return 12 * Size; } public static T TryFindParent&lt;T&gt;(DependencyObject child) where T : DependencyObject { //get parent item DependencyObject parentObject = GetParentObject(child); //we've reached the end of the tree if (parentObject == null) return null; //check if the parent matches the type we're looking for T parent = parentObject as T; if (parent != null) { return parent; } else { //use recursion to proceed with next level return TryFindParent&lt;T&gt;(parentObject); } } public static DependencyObject GetParentObject(DependencyObject child) { if (child == null) return null; //handle content elements separately ContentElement contentElement = child as ContentElement; if (contentElement != null) { DependencyObject parent = ContentOperations.GetParent(contentElement); if (parent != null) return parent; FrameworkContentElement fce = contentElement as FrameworkContentElement; return fce != null ? fce.Parent : null; } //also try searching for parent in framework elements (such as DockPanel, etc) FrameworkElement frameworkElement = child as FrameworkElement; if (frameworkElement != null) { DependencyObject parent = frameworkElement.Parent; if (parent != null) return parent; } //if it's not a ContentElement/FrameworkElement, rely on VisualTreeHelper return VisualTreeHelper.GetParent(child); } } </code></pre> <p>Sample Usage</p> <pre><code>xmlns:my="clr-namespace:FontSizeExample" &lt;TextBlock Text="Sample Font" FontSize="{my:EmFontSize 1.1}"/&gt; &lt;TextBlock Text="Sample Font" FontSize="{my:EmFontSize .9}"/&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload