2009-03-17 26 views

Trả lời

2

AFAIK, hiện không có sự kiện nào. Nhưng bạn có thể thực hiện mong muốn của mình cho điều này được biết đến here.

Một giải pháp thay thế (và tôi cũng không biết liệu điều này có khả thi hay không) sẽ đo kích thước phông chữ mong muốn lớn như thế nào, sau đó lấy nó làm đơn vị "ems", sau đó mở rộng bằng cách sử dụng "đơn vị" đó.

3
<ScaleTransform ScaleX="1.2" ScaleY="1.2"></ScaleTransform> 

dường như ít nhiều là lựa chọn thay thế 1.2em.

1

Thật không may, không có tương đương trong WPF của đơn vị em. Tất cả kích thước phông chữ của bạn, v.v. luôn được đặt bằng pixel.

5

em kích thước là chiều rộng của chữ M hoa trong phông chữ hiện tại, không có phương pháp định cỡ font-phụ thuộc trong WPF

Btw, WPF sử dụng "thiết bị pixel độc lập" mà luôn luôn 1/96 của một inch (vì đó là một điểm ảnh trên màn hình hiện nay) như vậy:

  • 1 điểm ảnh là 1/96 inch
  • 96 pixel trong một inch
  • 1,33333 pixel là một điểm
  • 3,779 p ixels là mm

Chúng cực kỳ không chính xác trên màn hình vì hầu như tất cả màn hình đều báo cáo độ phân giải 96DPI và bỏ qua kích thước pixel thực nhưng rất hữu ích khi in.

7

Đây là những gì tôi đã làm. Đã tạo một MarkupExtension chuyển đổi kích thước phông chữ thành EM dựa trên phông chữ được gán trên Window.

Tôi muốn gửi lời cảm ơn http://10rem.net/blog/2011/03/09/creating-a-custom-markup-extension-in-wpf-and-soon-silverlight

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

cho việc cung cấp kiến ​​thức cần thiết.

[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); 
    } 
} 

Sample Cách sử dụng

xmlns:my="clr-namespace:FontSizeExample" 
<TextBlock Text="Sample Font" FontSize="{my:EmFontSize 1.1}"/> 
<TextBlock Text="Sample Font" FontSize="{my:EmFontSize .9}"/> 
Các vấn đề liên quan