在代码中设置 Style 属性 - 依赖属性 FontSizeProperty 名称在 silverlight 库的当前上下文中不存在

发布于 2024-11-26 03:04:10 字数 1535 浏览 1 评论 0原文

这与我之前的问题类似,但该解决方案并没有解决这个问题。

当我将方法从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

临风闻羌笛 2024-12-03 03:04:10

我相信您需要 Control.FontSizePropertyControl.FontWeightProperty

您的 MainPage 是一个用户控件,它具有 Control 作为超类,因此继承了上述两个依赖属性。您的静态类不是 Control 的子类,因此它不会继承这些依赖属性。

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.

英雄似剑 2024-12-03 03:04:10

FontSizeProperty 是在 Control 上定义的,您不是从中派生的,因此您必须使用 Control.FontSizeProperty

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文