Answered by:
WPF converting a string into a FontWeight

Question
-
Hey guys,
Anybody know how to set 'FontWeight' through a variable? I have a program that records this (and other) information. I store the value that is held just as "Bold" or "Normal" and then I want to be able to reload it later. I can't figure out how to convert "Bold" to the fontweight Bold that can be used for a run. The value will be held in a variable that I need to use during the conversion. Thanks guys.
Wednesday, March 13, 2013 7:07 PM
Answers
-
You can use ToString() to convert the 'FontWeight' to a string, but there doesn't seem to be an easy way to reverse it. The only thing I can think to do is to write an IValueConverter function that converts between a string an a System.Windows.FontWeight object. It would look something like the following, although I only added a few of the possible font weights:
[ValueConversion(typeof(string), typeof(System.Windows.FontWeight))] public class FontConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { FontWeight fontWt; switch (value.ToString()) { case "Bold": fontWt = FontWeights.Bold; break; case "ExtraBold": fontWt = FontWeights.ExtraBold; break; case "Normal": fontWt = FontWeights.Normal; break; case "Light": fontWt = FontWeights.Light; break; default: fontWt = FontWeights.Normal; break; } return fontWt; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { FontWeight fontWt = (FontWeight)value; return fontWt.ToString(); } }
This would be able to be used in the XAML. If you only need the conversion in code, you could just use the Convert part of this as a function in code--something like
public System.Windows.FontWeight ConvertFontWeight(string strFontWeight) { // previous code }
Hope this helps!
Christine A. Piffat
- Marked as answer by JShirley-NCF Thursday, March 14, 2013 11:31 AM
Wednesday, March 13, 2013 8:27 PM
All replies
-
Hi try using this inside a converter -
string sWeight = "Bold"; FontWeight fw = (FontWeight)new FontWeightConverter().ConvertFromString(sWeight);
set a public property in your code to take your value and return the fw (FontWeight) your run can then bind its fontweight to your fontweight property.
Another option is to create a converter and put the code above inside that. Then you can use a converter to convert the string value "Bold" or "normal" and that will return a fontweight for you.
- Proposed as answer by Stepe Saturday, August 29, 2015 2:11 PM
Wednesday, March 13, 2013 8:18 PM -
You can use ToString() to convert the 'FontWeight' to a string, but there doesn't seem to be an easy way to reverse it. The only thing I can think to do is to write an IValueConverter function that converts between a string an a System.Windows.FontWeight object. It would look something like the following, although I only added a few of the possible font weights:
[ValueConversion(typeof(string), typeof(System.Windows.FontWeight))] public class FontConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { FontWeight fontWt; switch (value.ToString()) { case "Bold": fontWt = FontWeights.Bold; break; case "ExtraBold": fontWt = FontWeights.ExtraBold; break; case "Normal": fontWt = FontWeights.Normal; break; case "Light": fontWt = FontWeights.Light; break; default: fontWt = FontWeights.Normal; break; } return fontWt; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { FontWeight fontWt = (FontWeight)value; return fontWt.ToString(); } }
This would be able to be used in the XAML. If you only need the conversion in code, you could just use the Convert part of this as a function in code--something like
public System.Windows.FontWeight ConvertFontWeight(string strFontWeight) { // previous code }
Hope this helps!
Christine A. Piffat
- Marked as answer by JShirley-NCF Thursday, March 14, 2013 11:31 AM
Wednesday, March 13, 2013 8:27 PM -
Thanks guys,
I just didn't know if there was some way to do a direct conversion or if there was a method that I hadn't seen that would accept a string when defining fontweight. I always feel stupid when I create a conversion function just to find out that I could have just used CType or the like.
Thanks
Thursday, March 14, 2013 11:33 AM -
I'm quite new to wpf and was really annoyed because of the missing stuff I was used to in windows forms.
So thanks for pointing me to these converters (there are converters for all typeface properties)- its still awkward without having a font class but still much easier than creating these self made converter functions.
Cheers, Stephan
To life is christ, to die is gain...
Saturday, August 29, 2015 2:18 PM -
WPF is totally different from windows forms.
If you approach it with the plan to directly translate from windows forms code to wpf then you may as well just stop and go back to winforms.
With wpf think binding.
For example:
https://gallery.technet.microsoft.com/WPF-Dynamic-Fonts-ad3741ca
Saturday, August 29, 2015 4:00 PM