在代码中设置 Style 属性 - 依赖属性 FontSizeProperty 名称在 silverlight 库的当前上下文中不存在
这与我之前的问题类似,但该解决方案并没有解决这个问题。
当我将方法从 Silverlight MainPage 代码后面(有效)移动到 silverlight 库中的新类时,fontSizeProperty 未被识别
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;
}
}
}
注意:根据 MSDN 对于 FontSizeProperty,我确实包含了 System.Windows 参考和“使用 System.Windows. Control”
根据下面的答案,我将“Property = FontSizeProperty”更改为“Property=DataGridColumnHeader.FontSizeProperty”等,如下所示:
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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信您需要
Control.FontSizeProperty
和Control.FontWeightProperty
。您的
MainPage
是一个用户控件,它具有Control
作为超类,因此继承了上述两个依赖属性。您的静态类不是Control
的子类,因此它不会继承这些依赖属性。I believe you want
Control.FontSizeProperty
andControl.FontWeightProperty
instead.Your
MainPage
is a user control, which hasControl
as a superclass and hence inherits the above two dependency properties. Your static class isn't a subclass ofControl
so it doesn't inherit these dependency properties.FontSizeProperty
是在Control
上定义的,您不是从中派生的,因此您必须使用Control.FontSizeProperty
。FontSizeProperty
is defined onControl
, which you do not derive from, so you have to useControl.FontSizeProperty
.