0

I am designing a control that can have two main states. The state will be set via a dependence property. I would like to set the property via XAML. I know that the setter will only run the setValue code if it is set via XAML as explained here: WPF: XAML property declarations not being set via Setters?

So I've tried to set up a DependencyPropertyChangedEvent event, but when I run my code I get this error:

XamlParseException Occurred
'The invocation of the constructor on type 'RouteOneClient.VehicleInfoControl'
that matches the specified binding constraints threw an exception.' Line number 
'134' and line position '34'.

When I look through that, it tells me that I cannot create a new instance of my control in my event. The C# logic looks like this:

    public enum VehicleType
    {
        Sale = 1,
        Trade = 2,
    }

    public static readonly DependencyProperty VehicleTypeProperty =  DependencyProperty.Register(
        "VehicleTYPE", 
        typeof(VehicleType), 
        typeof(VehicleInfoControl), 
        new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender, new PropertyChangedCallback(OnVehTypeChanged)));  // optionally metadata for defaults etc.

    public VehicleInfoControl()
    {
        InitializeComponent();
    }

    public VehicleType VehicleTYPE
    {
        get { return (VehicleType)GetValue(VehicleTypeProperty); }
        set
        {
            SetValue(VehicleTypeProperty, value);
        }          
    }

    private static void OnVehTypeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        VehicleInfoControl instance = d as VehicleInfoControl;

        if (instance.VehicleTYPE == VehicleType.Sale)
        {
            instance.TitleLabel.Content = "Sale Vehicle";
            instance.IntendedUse.Visibility = System.Windows.Visibility.Visible;
            instance.VehicleNewUsed.Visibility = System.Windows.Visibility.Visible;
            instance.VehicleVin.Visibility = System.Windows.Visibility.Visible;
            instance.VehicleInceptionMiles.Visibility = System.Windows.Visibility.Visible;
            instance.IntendedUseLabel.Visibility = System.Windows.Visibility.Visible;
            instance.VehicleNewUsedLabel.Visibility = System.Windows.Visibility.Visible;
            instance.VehicleVinLabel.Visibility = System.Windows.Visibility.Visible;
            instance.VehicleInceptionMilesLabel.Visibility = System.Windows.Visibility.Visible;
        }
        else
        {
            instance.TitleLabel.Content = "Trade Vehicle";
            instance.IntendedUse.Visibility = System.Windows.Visibility.Hidden;
            instance.VehicleNewUsed.Visibility = System.Windows.Visibility.Hidden;
            instance.VehicleVin.Visibility = System.Windows.Visibility.Hidden;
            instance.VehicleInceptionMiles.Visibility = System.Windows.Visibility.Hidden;
            instance.IntendedUseLabel.Visibility = System.Windows.Visibility.Hidden;
            instance.VehicleNewUsedLabel.Visibility = System.Windows.Visibility.Hidden;
            instance.VehicleVinLabel.Visibility = System.Windows.Visibility.Hidden;
            instance.VehicleInceptionMilesLabel.Visibility = System.Windows.Visibility.Hidden;
        }
    }

And the XAML:

<Expander Header="Vehicle Information" Width="Auto" Height="Auto" Grid.Row="2">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width=".3*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>

        <custom:VehicleInfoControl x:Name="SaleVeh" Width="Auto" Height="Auto" Grid.Column="0" VehicleTYPE="Sale"/>
        <custom:VehicleInfoControl x:Name="TradeVeh" Width="Auto" Height="Auto" Grid.Column="2" VehicleTYPE="Trade"/>

    </Grid>
</Expander>

The error given above points to the line.

I feel like I'm following the form correctly, but it isn't running. Any ideas?

Community
  • 1
  • 1
Nathan Tornquist
  • 6,468
  • 10
  • 47
  • 72

2 Answers2

3

The type of your dependency property is an enum, which is a ValueType, but you specify null as default property value in your property metadata. That won't work, since null is not a valid value for a ValueType.

Clemens
  • 123,504
  • 12
  • 155
  • 268
2

While registering your dependency property you are passing "null" into "FrameworkPropertyMetadata" constructor.

The first parameter you are passing will be default value for your Dependency property. In your case you are passing Null as default value for a dependency property of type ENUM, which is not allowed.

passing a valid default value may fix your problem..

some thing like this may do

new FrameworkPropertyMetadata(VehicleType.Sale,  FrameworkPropertyMetadataOptions.AffectsRender,  new PropertyChangedCallback(OnVehTypeChanged)));
Bathineni
  • 3,436
  • 18
  • 25