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 = DataGridCo开发者_运维百科lumnHeader.ContentTemplateProperty, Value = CreateDataGridHeaderTemplate(tooltip) });
return newGridHeaderStyle;
}
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.
FontSizeProperty
is defined on Control
, which you do not derive from, so you have to use Control.FontSizeProperty
.
精彩评论