views:

86

answers:

1

I am learning WPF and am trying to create my first UserControl. My UserControl consists of

  1. StackPanel
  2. StackPanel contains a Label and TextBox

I am trying to create two Dependency Properties

  1. Text for the Label
  2. Orientation for the StackPanel - The orientation will affect the position of the Label and TextBox effectively

I have successfully created a Text dependency property and bind it to my UserControls . But when I created the Orientation property, I seem to get following error in get property

The as operator must be used with a reference type or nullable type ('System.Windows.Controls.Orientation' is a non-nullable value type)

public static DependencyProperty OrientationProperty = DependencyProperty.Register("Orientation", typeof(System.Windows.Controls.Orientation), typeof(MyControl), new PropertyMetadata((System.Windows.Controls.Orientation)(Orientation.Horizontal)));
public Orientation Orientation
{
    get { return GetValue(OrientationProperty) as System.Windows.Controls.Orientation; }
    set { SetValue(OrientationProperty, value); }
}

Appreciate your help.

Edit: I changed the code as below and it seem to work as expected. But is this the correct way to solve the problem?

public Orientation Orientation  
{
        get 
        {
            Orientation? o = GetValue(OrientationProperty) as System.Windows.Controls.Orientation?;
            if (o.HasValue)
            {
                return (System.Windows.Controls.Orientation)o.Value;
            }
            else
            {
                return Orientation.Horizontal;
            }
        }
        set { SetValue(OrientationProperty, value); }
    }      
+4  A: 

The error message says it all. The as operator can only be used with a Type that is nullable (reference type, or Nullable<T>), because it will return either the value cast, or null.

What you're trying to use it on is an enumeration.

Just use a regular cast:

get { return (System.Windows.Controls.Orientation) GetValue(OrientationProperty); } 

Reasons why:

  1. You define a default value in your DependencyProperty.Register call, eliminating any default null value
  2. Your DependencyProperty is typeof(Orientation), which doesn't allow for nulls
  3. Your class's property definition is Orientation, which doesn't allow for nulls
  4. Any attempt to set an invalid value via direct calls to SetValue(OrientationProperty, null) will receive an exception, so your property getter won't ever see a null value even by a naughty user of it.
Adam Sills
beat me to it :)
Mark Synowiec
I have just added the changes I made in code. Could you comment on my changes please? Should I direct cast or use the method that I used?
byte
Just use a cast. Your property doesn't allow nullable values. (You didn't define the dependency property as Orientation?, nor did you define your property itself as Orientation?, so there's no need for the as keyword. Especially since you provide a default value of Orientation.Horizontal in the Dependency Property definition, so there's no nullability defined.
Adam Sills
Thanks Adam. I used 'as' because I saw it in one of the articles on Dependency Property and they used as on a String return type. I have already voted your answer and now I'll mark is so. Appreciate your help.
byte