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?