Reputation: 220
I can use:
<TextBlock Text="Some Textes" FontSize="12pt" />
It works normally. But I want to use the DynamicResource
extension, is it possible?
This does not work:
<TextBlock Text="Some Textes" FontSize="{DynamicResource SomeResources}" />
SomeResources:
<System:Stringx:Key="SomeResources">12pt</System:String>
I do not want to use an AttachedBehavior
. I know, I can solve my problem with it (using FontSizeConverter
).
Upvotes: 2
Views: 4865
Reputation: 17380
Update:
I see you've edited your question and taken out the binding option. If your only wanting to get this from a xaml resource, you'd need to use a MarkupExtension
. You can find the MarkupExtension
and the usage here. This will work fine for your case.
Orig reply:
FontSize
is of type System:Double
Documentation.
Next default Binding
for FontSize
assumes pixels at device independent scale but since you need pt's we can use a converter such as:
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();
}
}
and usage:
<TextBlock FontSize="{Binding StringProperty, Converter={StaticResource ConvertFromPointConverter}}">Some Text</TextBlock>
Alternate:
If you do not want to use converters and FontSizeConverter
just do the calculation in your property getter.
something like:
private double _someFontval;
public double SomeFontVal {
get {
return _someFontval * 96 / 72;
}
set {
_someFontval = value;
}
}
usage:
//.cs
SomeFontVal = 12.0;
//.xaml
<TextBlock FontSize="{Binding SomeFontVal}">Some Text</TextBlock>
Upvotes: 2
Reputation: 5465
I believe you can just bind it as a int double
<System:Double x:Key="SomeResources">12</System:Double>
Upvotes: 0