依赖项属性,无法创建新的类实例
本文关键字:实例 创建 属性 依赖 | 更新日期: 2023-09-27 18:35:30
我正在设计一个可以有两个主要状态的控件。 状态将通过依赖项属性设置。 我想通过 XAML 设置属性。 我知道如果通过 XAML 设置了 setValue 代码,则资源库只会运行该代码,如下所述:WPF:XAML 属性声明不是通过 Sseters 设置的?
所以我尝试设置一个 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>
上面给出的错误指向该行。
我觉得我正确地遵循了表单,但它没有运行。 有什么想法吗?
依赖项
属性的类型是枚举,枚举是 ValueType,但您可以在属性元数据中指定null
作为默认属性值。这是行不通的,因为null
不是 ValueType 的有效值。
在注册依赖项属性时,将"null"传递到"FrameworkPropertyMetadata"构造函数中。
要传递的第一个参数将是依赖项属性的默认值。在您的情况下,您将 Null 作为 ENUM 类型的依赖项属性的默认值传递,这是不允许的。
传递有效的默认值可能会解决您的问题。
像这样的事情可能会做
new FrameworkPropertyMetadata(VehicleType.Sale, FrameworkPropertyMetadataOptions.AffectsRender, new PropertyChangedCallback(OnVehTypeChanged)));