Brad Boyce
Brad Boyce

Reputation: 1258

Setting Style property in code - dependency property FontSizeProperty name does not exist in current context in silverlight library

This is similar to my previous question, but that solution did not solve this problem.

fontSizeProperty is not being recognized when I move a method from my Silverlight MainPage code behind (which worked) to a new class in a silverlight library

using System.Windows.Controls;

namespace MyNameSpace
{
    public static class DataGridBuilder
    {
        private static Style BuildHeaderStyle(string tooltip)
        {
            Style newGridHeaderStyle = new Style(typeof(DataGridColumnHeader));
            newGridHeaderStyle.Setters.Add(new Setter { Property = FontSizeProperty, Value = 9.0 });
            newGridHeaderStyle.Setters.Add(new Setter { Property = FontWeightProperty, Value = FontWeights.Bold });
            return newGridHeaderStyle;
        }
    }
}

NOTE: Per MSDN for FontSizeProperty, I do include System.Windows reference, and "using System.Windows.Control"

Based on answers below, I changed "Property = FontSizeProperty" to "Property=DataGridColumnHeader.FontSizeProperty" etc., like this:

    private static Style BuildHeaderStyle(string tooltip)
    {
        FontWeight fw = FontWeights.Bold;
        Style newGridHeaderStyle = new Style(typeof(DataGridColumnHeader));
        newGridHeaderStyle.Setters.Add(new Setter { Property = DataGridColumnHeader.FontSizeProperty, Value = 9.0 });
        newGridHeaderStyle.Setters.Add(new Setter { Property = DataGridColumnHeader.FontWeightProperty, Value = FontWeights.Bold });
        newGridHeaderStyle.Setters.Add(new Setter { Property = DataGridColumnHeader.ContentTemplateProperty, Value = CreateDataGridHeaderTemplate(tooltip) });
        return newGridHeaderStyle;
    }

Upvotes: 0

Views: 1225

Answers (2)

Luke Woodward
Luke Woodward

Reputation: 64959

I believe you want Control.FontSizeProperty and Control.FontWeightProperty instead.

Your MainPage is a user control, which has Control as a superclass and hence inherits the above two dependency properties. Your static class isn't a subclass of Control so it doesn't inherit these dependency properties.

Upvotes: 1

Julien Lebosquain
Julien Lebosquain

Reputation: 41243

FontSizeProperty is defined on Control, which you do not derive from, so you have to use Control.FontSizeProperty.

Upvotes: 1

Related Questions