我正在设计一个可以有两种主要状态的控件。状态将通过依赖属性设置。我想通过 XAML 设置属性。我知道 setter 仅在通过 XAML 设置时才会运行 setValue 代码,如下所述: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>
上面给出的错误指向该行。
我觉得我正确地遵循了表格,但它没有运行。有任何想法吗?