views:

148

answers:

1

I have the following simple TextBox subclass, which adds one dependency property (OutputIndex):

public class OutputTextBox : TextBox
{
    public OutputTextBox() : base() { }

    public int OutputIndex
    {
        get { return (int)this.GetValue(OutputIndexProperty); }
        set { this.SetValue(OutputIndexProperty, value); }
    }

    public static readonly DependencyProperty OutputIndexProperty = DependencyProperty.Register(
      "OutputIndex", 
      typeof(int), 
      typeof(OutputTextBox), 
      new PropertyMetadata(false));
}

When I try to instantiate an instance of OutputTextBox, like

OutputTextBox otb = new OutputTextBox();

I get a System.TypeInitializationException thrown with the InnerException saying: "Default value type does not match type of property 'OutputIndex'."

What 'Default value type' is the InnerException referring to? What do I need to do to be able to instantiate an instance of OutputTextBox?

+1  A: 

Your PropertyMetadata is set to initialize OutputIndex with a default value of false. False is a boolean, OutputIndex is an int, hence the type exception.

Pass an integer argument to PropertyMetadata, or leave it blank for no default value.

Charlie
Of course - thanks! That's what I get for doing cut and paste without reading the documentation in detail.
Philipp Schmid