当前位置:  开发笔记 > 前端 > 正文

WPF类比'em'单位

如何解决《WPF类比'em'单位》经验,为你挑选了2个好方法。

CSS em单元的WPF类比是什么?



1> 小智..:

这就是我做的.创建了一个MarkupExtension,它根据Window上指定的字体将Font size转换为EM.

我要感谢 http://10rem.net/blog/2011/03/09/creating-a-custom-markup-extension-in-wpf-and-soon-silverlight

http://tomlev2.wordpress.com/tag/markup-extension/

提供所需的知识.

[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(targetObject);
        if (window != null)
        {
            return window.FontSize * Size;
        }
        return 12 * Size;
    }

    public static T TryFindParent(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(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);
    }
}

样本用法

xmlns:my="clr-namespace:FontSizeExample"




2> Nir..:

em size是当前字体中大写字母M的宽度,Wpf中没有与字体相关的大小调整方法

顺便说一句,WPF使用"设备独立像素",总是1/96英寸(因为这是今天显示器上的一个像素)所以:

1像素是1/96英寸

一英寸96像素

1.33333像素是一个点

3.779像素是mm

这些在监视器上非常不准确,因为几乎所有监视器都报告96DPI分辨率并忽略实际像素大小,但在打印时非常有用.

推荐阅读
赛亚兔备_393
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有