c# - 如何在 C# 中的 FontWeights 之间使用等于运算符?

标签 c# fonts custom-controls

我必须在我的 CustomControl 中创建一个 FontWeight 属性,但它在控制 FontWeights 时出错。我该如何解决?

属性(property)登记:

public static readonly DependencyProperty FontWeightProperty =
        DependencyProperty.Register(
            "FontWeight",
            typeof(int),
            typeof(WatermarkTextBox),
        new PropertyMetadata(0));

Property(我们可以改变这个属性,这是我的业余工作):

    private int _watermarkFontWeight = 0;

    public int WatermarkFontWeight
    {
        get
        {
            if (watermarkPassTextBox.FontWeight == FontWeights.Normal)
            {
                _watermarkFontWeight = 0;
            }
            else if (watermarkPassTextBox.FontWeight == FontWeights.SemiBold)
            {
                _watermarkFontWeight = 1;
            }
            else if (watermarkPassTextBox.FontWeight == FontWeights.Bold)
            {
                _watermarkFontWeight = 2;
            }
            else if (watermarkPassTextBox.FontWeight == FontWeights.ExtraBold)
            {
                _watermarkFontWeight = 3;
            }
            return _watermarkFontWeight;
        }
        set
        {
            if (value == 0)
            {
                SetProperty<int>(ref _watermarkFontWeight, value, "FontWeight");
                watermarkPassTextBox.FontWeight = FontWeights.Normal;
            }
            else if (value == 1)
            {
                SetProperty<int>(ref _watermarkFontWeight, value, "FontWeight");
                watermarkPassTextBox.FontWeight = FontWeights.SemiBold;
            }
            else if (value == 2)
            {
                SetProperty<int>(ref _watermarkFontWeight, value, "FontWeight");
                watermarkPassTextBox.FontWeight = FontWeights.Bold;
            }
            else if (value == 3)
            {
                SetProperty<int>(ref _watermarkFontWeight, value, "FontWeight");
                watermarkPassTextBox.FontWeight = FontWeights.ExtraBold;
            }
        }
    }

错误:

Operator '==' cannot be applied to operands of type 'Windows.UI.Text.FontWeight' and 'Windows.UI.Text.FontWeight'

谢谢。

最佳答案

documentation page 中我可以看出(这令人惊讶地缺乏信息和/或我无法找到详细的 decoumentation),FontWeight 是一个 struct 值类型,== 定义一个运算符,这样你就不能直接比较它们。

但是,我认为您可以比较它们的底层包装 Weight 值:

if (watermarkPassTextBox.FontWeight.Weight == FontWeights.Normal.Weight)

编辑:我不确定他们的 Equals 实现是否有效(同样,可爱的文档),但您可以创建一个扩展方法来为您提供一些看起来不错的语法:

public static bool Equals(this FontWeight weight1, FontWeight weight2)
{
    return weight1.Weight == weight2.Weight;
}

这导致使用:

if (watermarkPassTextBox.FontWeight.Equals(FontWeights.Normal))
    _watermarkFontWeight = 0;
else if (watermarkPassTextBox.FontWeight.Equals(FontWeights.SemiBold))
    _watermarkFontWeight = 1;
else if (watermarkPassTextBox.FontWeight.Equals(FontWeights.Bold))
    _watermarkFontWeight = 2;
else if (watermarkPassTextBox.FontWeight.Equals(FontWeights.ExtraBold))
    _watermarkFontWeight = 3;
else
    return _watermarkFontWeight;

关于c# - 如何在 C# 中的 FontWeights 之间使用等于运算符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18188053/

相关文章:

c# - 从 Entity Framework Core IQueryable<T> 获取 SQL 代码

c# - ASP.NET GridView : Highlighting rows on mouse over then updating the highlighted rows

c# - 在我的 C# XAML Windows 8.1 应用程序中,如何进行编程绑定(bind)?

css - Font Awesome 图标不显示

ios - 如何在不更改xcode中的字体系列的情况下更改标签的字体大小

delphi - 如何从 TCustomControl 中制作出 TScrollbox?

c# - 紧凑型框架上的 GDI RoundRect : make rounded rectangle's outside transparent

c# - 从 Decimal 类型到 Single 类型的转换是缩小转换还是扩大转换?

java - 从 JComboBox 更改 Graphics2D 字体

wpf - 自定义控件不继承父级的样式