예제 #1
0
파일: StyleModel.cs 프로젝트: Gigas002/Jaml
        /// <inheritdoc />
        public T ToStyle <T>() where T : Style, new()
        {
            //todo improve this ugly code?
            T style = new T();

            FontWeight fontWeight      = PropertyParser.ParseFontWeight(FontWeight);
            FontStyle  fontStyle       = PropertyParser.ParseFontStyle(FontStyle);
            Brush      foreground      = PropertyParser.ConvertArgbToBrush(Foreground);
            Brush      background      = PropertyParser.ParseBackground(Background);
            Thickness  borderThickness = new Thickness(BorderThickness);
            bool       isParsed        = Enum.TryParse(Visibility, out Visibility visibility);

            visibility = isParsed ? visibility : default;

            style.Setters.Add(new Setter {
                Property = Control.FontWeightProperty, Value = fontWeight
            });
            style.Setters.Add(new Setter {
                Property = Control.FontStyleProperty, Value = fontStyle
            });
            style.Setters.Add(new Setter {
                Property = Control.FontSizeProperty, Value = FontSize
            });
            style.Setters.Add(new Setter {
                Property = Control.ForegroundProperty, Value = foreground
            });
            style.Setters.Add(new Setter {
                Property = Control.BackgroundProperty, Value = background
            });
            style.Setters.Add(new Setter {
                Property = Control.BorderThicknessProperty, Value = borderThickness
            });
            style.Setters.Add(new Setter {
                Property = UIElement.VisibilityProperty, Value = visibility
            });

            if (string.IsNullOrWhiteSpace(FontFamily))
            {
                return(style);
            }

            style.Setters.Add(new Setter
            {
                Property = Control.FontFamilyProperty,
                Value    = new FontFamily(PathsHelper.GetUriFromRelativePath(FontFamily), string.Empty)
            });

            return(style);
        }