P
P
postya2019-12-25 23:19:21
WPF
postya, 2019-12-25 23:19:21

How to set label font from settings file with less code?

Application in C# + WPF
There are 8 labels and 20 fonts
When the program is loaded, the font names are taken from the settings file and, depending on the font names, each label is assigned the appropriate font
I have if else conditions, everything works, but the code is sooo much, I would like refactoring to do, and to make the code smaller in some other way
How can this be done?

//переменные в которые помещаются названия шрифтов для каждого лейбла
           var fontCategory1 = Settings.Default.LabelCategoryFont1;
            var fontCategory2 = Settings.Default.LabelCategoryFont2;
            var fontCategory3 = Settings.Default.LabelCategoryFont3;
            var fontCategory4 = Settings.Default.LabelCategoryFont4;
            var fontCategory5 = Settings.Default.LabelCategoryFont5;
            var fontCategory6 = Settings.Default.LabelCategoryFont6;
            var fontCategory7 = Settings.Default.LabelCategoryFont7;
            var fontCategory8 = Settings.Default.LabelCategoryFont8;

conditions in which, depending on the name of the font, a font is assigned to each label. Here are the conditions for only two labels and two fonts, and there will be 8 and 20 fonts in total, you can imagine what kind of footcloth it will be ineffective with if conditions
if (fontCategory1 == "Heron Sans Light")
            {
                ApplyFontProperties(CategoryLabel1, new FontFamily("HeronSans"), FontWeights.Light,
                    FontStyles.Normal);
            }
            
            if (fontCategory2 == "Heron Sans Light")
            {
                ApplyFontProperties(CategoryLabel2, new FontFamily("HeronSans"), FontWeights.Light,
                    FontStyles.Normal);
            }
            
            if (fontCategory1 == "Heron Sans Bold")
            {
                ApplyFontProperties(CategoryLabel1, new FontFamily("HeronSans"), FontWeights.Bold,
                    FontStyles.Normal);
            }
            
            if (fontCategory2 == "Heron Sans Bold")
            {
                ApplyFontProperties(CategoryLabel2, new FontFamily("HeronSans"), FontWeights.Bold,
                    FontStyles.Normal);
            }

The ApplyFontProperties method assigns a font and styles to each label, depending on which label was received as input:
private void ApplyFontProperties(Label label, FontFamily fontFamily, FontWeight fontWeight, FontStyle fontStyle)
        {
            if (label == CategoryLabel1)
            {
                CategoryLabel1.FontFamily = fontFamily;
                CategoryLabel1.FontStyle = fontStyle;
                CategoryLabel1.FontWeight = fontWeight;
            }
            if (label == CategoryLabel2)
            {
                CategoryLabel2.FontFamily = fontFamily;
                CategoryLabel2.FontStyle = fontStyle;
                CategoryLabel2.FontWeight = fontWeight;
            }
            if (label == CategoryLabel3)
            {
                CategoryLabel3.FontFamily = fontFamily;
                CategoryLabel3.FontStyle = fontStyle;
                CategoryLabel3.FontWeight = fontWeight;
            }
            if (label == CategoryLabel4)
            {
                CategoryLabel4.FontFamily = fontFamily;
                CategoryLabel4.FontStyle = fontStyle;
                CategoryLabel4.FontWeight = fontWeight;
            }
            if (label == CategoryLabel5)
            {
                CategoryLabel5.FontFamily = fontFamily;
                CategoryLabel5.FontStyle = fontStyle;
                CategoryLabel5.FontWeight = fontWeight;
            }
            if (label == CategoryLabel6)
            {
                CategoryLabel6.FontFamily = fontFamily;
                CategoryLabel6.FontStyle = fontStyle;
                CategoryLabel6.FontWeight = fontWeight;
            }
            if (label == CategoryLabel7)
            {
                CategoryLabel7.FontFamily = fontFamily;
                CategoryLabel7.FontStyle = fontStyle;
                CategoryLabel7.FontWeight = fontWeight;
            }
            if (label == CategoryLabel8)
            {
                CategoryLabel8.FontFamily = fontFamily;
                CategoryLabel8.FontStyle = fontStyle;
                CategoryLabel8.FontWeight = fontWeight;
            }

The settings file, which stores font names. string data format:
<setting name="LabelCategoryFont1" serializeAs="String">
           <value>Heron Sans Light</value>
  </setting>

   <setting name="LabelCategoryFont2" serializeAs="String">
            <value>Heron Sans Bold</value>
    </setting>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman, 2019-12-26
@yarosroman

MVVM and bindings will save you. For wpf, working with a form directly is bad manners.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question