【发布时间】:2010-10-13 19:49:50
【问题描述】:
CSS em 单元的 WPF 类比是什么?
【问题讨论】:
标签: css wpf units-of-measurement
CSS em 单元的 WPF 类比是什么?
【问题讨论】:
标签: css wpf units-of-measurement
AFAIK,目前还没有。但是你可以让你知道here。
另一种选择(我也不知道这是否可能)是测量所需字体的大小,然后将其作为“ems”单位,然后使用这些“单位”进行缩放。
【讨论】:
<ScaleTransform ScaleX="1.2" ScaleY="1.2"></ScaleTransform>
似乎或多或少是 1.2em 的替代品。
【讨论】:
不幸的是,在 em 单元的 WPF 中没有等效项。您所有的字体大小等始终以像素为单位设置。
【讨论】:
em size是当前字体中大写字母M的宽度,Wpf中没有依赖字体的大小方法
顺便说一句,WPF 使用始终为 1/96 英寸的“设备无关像素”(因为在今天的显示器上这是一个像素)所以:
这些在显示器上非常不准确,因为几乎所有显示器都报告 96DPI 分辨率并忽略实际像素大小,但在打印时非常有用。
【讨论】:
这就是我所做的。创建了一个 MarkupExtension,它根据 Window 上分配的字体将字体大小转换为 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<Window>(targetObject);
if (window != null)
{
return window.FontSize * Size;
}
return 12 * Size;
}
public static T TryFindParent<T>(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<T>(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"
<TextBlock Text="Sample Font" FontSize="{my:EmFontSize 1.1}"/>
<TextBlock Text="Sample Font" FontSize="{my:EmFontSize .9}"/>
【讨论】: