2

我可以用:

<TextBlock Text="Some Textes" FontSize="12pt" />

它工作正常。但是我想使用DynamicResource扩展,可以吗?

这不起作用:

<TextBlock Text="Some Textes" FontSize="{DynamicResource SomeResources}" /> 

一些资源:

<System:Stringx:Key="SomeResources">12pt</System:String>

我不想使用AttachedBehavior. 我知道,我可以用它解决我的问题(使用FontSizeConverter)。

4

2 回答 2

2

更新:

我看到您已经编辑了您的问题并删除了绑定选项。如果您只想从 xaml 资源中获取此信息,则需要使用MarkupExtension. 您可以在此处MarkupExtension找到和用法。这将适用于您的情况。

原回复:

FontSizeSystem:Double 文档类型。

下一个默认值Binding假设FontSize像素在设备独立比例,但由于您需要 pt,我们可以使用转换器,例如:

using System.Globalization;
using System.Windows;
using System.Windows.Data;

class ConvertFromPoint : IValueConverter
{
  public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
    var convertFrom = new FontSizeConverter().ConvertFrom(value.ToString());
    if (convertFrom != null)
      return (double) convertFrom;
    return 1;
  }

  public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
    throw new NotImplementedException();
  }
}

和用法:

<TextBlock FontSize="{Binding StringProperty, Converter={StaticResource ConvertFromPointConverter}}">Some Text</TextBlock>

备用:

如果您不想使用转换器,FontSizeConverter只需在属性 getter 中进行计算。

就像是:

private double _someFontval;
public double SomeFontVal {
  get {
    return _someFontval * 96 / 72;
  }

  set {
    _someFontval = value;
  }
}

用法:

//.cs
SomeFontVal = 12.0;

//.xaml
<TextBlock FontSize="{Binding SomeFontVal}">Some Text</TextBlock>
于 2013-05-27T13:42:37.760 回答
0

我相信您可以将其绑定为int double

<System:Double x:Key="SomeResources">12</System:Double>
于 2013-05-27T13:31:35.163 回答