依赖属性,无法创建新的类实例

时间:2012-05-25 13:07:56

标签: c# wpf xaml dependency-properties

我正在设计一个可以有两个主要状态的控件。州将通过依赖属性设置。我想通过XAML设置属性。我知道setter只会运行setValue代码,如果它是通过XAML设置的,如下所述:WPF: XAML property declarations not being set via Setters?

所以我尝试设置DependencyPropertyChangedEvent事件,但是当我运行我的代码时出现此错误:

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'.

当我查看它时,它告诉我在我的事件中无法创建我的控件的新实例。 C#逻辑看起来像这样:

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

和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>

上面给出的错误指向该行。

我觉得我正在正确地关注表单,但它没有运行。有什么想法吗?

2 个答案:

答案 0 :(得分:3)

您的依赖项属性的类型是枚举,它是ValueType,但您在属性元数据中将null指定为默认属性值。这不起作用,因为null不是ValueType的有效值。

答案 1 :(得分:2)

注册依赖项属性时,您将“null”传递给“FrameworkPropertyMetadata”构造函数。

您传递的第一个参数将是Dependency属性的默认值。 在您的情况下,您将Null作为类型为ENUM的依赖项属性的默认值传递,这是不允许的。

传递有效的默认值可能会解决您的问题..

有些事情可能会这样做

new FrameworkPropertyMetadata(VehicleType.Sale,  FrameworkPropertyMetadataOptions.AffectsRender,  new PropertyChangedCallback(OnVehTypeChanged)));