3

I have to create a FontWeight property in my CustomControl but it is giving me error in controlling FontWeights. How can I solve it?

Property Registering:

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

Property (We can change this property, it is my amateur job):

    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;
            }
        }
    }

Error:

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

Thanks.

Tugrul Emre Atalay
  • 918
  • 1
  • 9
  • 28

2 Answers2

4

From what I can tell from the documentation page (which is surprisingly lacking in information and/or I'm having trouble finding detailed decoumentation), FontWeight is a struct value type that does not define an operator for == so you can't directly compare them.

However, I think you can compare their underlying wrapped Weight value instead:

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

EDIT: I'm not sure if their Equals implementation works (again, lovely documentation), but you can create an extension method to give you some half-decent looking syntax:

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

Which results in usage:

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;
Chris Sinclair
  • 22,858
  • 3
  • 52
  • 93
  • 1
    @TuğrulEmreAtalay: No problem. Honestly, this all seems quite... weird to me the way that particular API is designed. Perhaps there's some reasoning behind it, but I don't know what it is. I've updated my answer to maybe give you a bit more manageable API. – Chris Sinclair Aug 12 '13 at 14:19
-1

I am not sure but you can try by comparing after Converting it To string.. i.e

if (Convert.Tostring(watermarkPassTextBox.FontWeight) == Convert.Tostring(FontWeights.Normal))
        {....}