How to read Windows.UI.XAML.Style properties in C #

I am writing a class that converts an HTML document to a Paragrpah list that can be used with RichTextBlock in Windows 8 applications. I want to give the class a list of styles defined in XAML, and the class will read the useful properties from the style and apply them.

If I have Windows.UI.XAML.Style style , how can I read a property from it? I tried

 var fontWeight = style.GetValue(TextElement.FontWeightProperty) 

for a style defined in XAML with TargetProperty = "TextBlock", but this fails and an exception

+4
source share
1 answer

You can try the following:

 var fontWeightSetter = style.Setters.Cast<Setter>().FirstOrDefault( setter => setter.Property == TextElement.FontWeightProperty); var fontWeight = fontWeightSetter != null ? (FontWeight)fontWeightSetter.Value : FontWeights.Normal; 

Or check if this works:

 public static class StyleExtensions { // Untested public static object GetPropertyValue(this Style style, DependencyProperty property) { var setter = style.Setters.Cast<Setter>().FirstOrDefault( s => s.Property == property); var value = setter != null ? setter.Value : null; if (setter == null && style.BasedOn != null) { value = style.BasedOn.GetPropertyValue(property); } return value; } } 
+2
source

Source: https://habr.com/ru/post/1442325/


All Articles