我可以用:
<TextBlock Text="Some Textes" FontSize="12pt" />正常工作。但是我想使用DynamicResource扩展,有可能吗?
这样做是行不通的:
<TextBlock Text="Some Textes" FontSize="{DynamicResource SomeResources}" /> SomeResources:
<System:Stringx:Key="SomeResources">12pt</System:String>我不想使用AttachedBehavior。我知道,我可以用它来解决我的问题(使用FontSizeConverter)。
发布于 2013-05-27 13:42:37
更新:
我看你修改了你的问题,去掉了绑定选项。如果您只想从xaml资源中获取这些信息,则需要使用MarkupExtension。您可以找到MarkupExtension和用法这里。这对你的案子没什么用。
答复:
FontSize是System: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>https://stackoverflow.com/questions/16774122
复制相似问题