I have defined a dependency property that uses an enum to update the background colour of a button. When it runs I get a “Default value type does not match type of property ‘CurrentWarningLevel'” exception. The dependency property is as follows:
public enum WarningLevels
{
wlError=0,
wlWarning,
wlInfo
};
public class WarningLevelButton : Button
{
static WarningLevelButton()
{
}
public WarningLevels CurrentWarningLevel
{
get { return (WarningLevels)GetValue(WarningLevelProperty); }
set { base.SetValue(WarningLevelProperty, value); }
}
public static readonly DependencyProperty WarningLevelProperty =
DependencyProperty.Register("CurrentWarningLevel", typeof(WarningLevels), typeof(WarningLevelButton), new PropertyMetadata(false));
}
I am trying to use the property as follows:
<Trigger Property="local:WarningLevelButton.CurrentWarningLevel" Value="wlError">
<Setter TargetName="ButtonBody" Property="Background" Value="{StaticResource CtlRedBrush}" />
<Setter TargetName="ButtonBody" Property="BorderBrush" Value="{StaticResource CtlRedBrush}" />
</Trigger>
<Trigger Property="local:WarningLevelButton.CurrentWarningLevel" Value="wlWarning">
<Setter TargetName="ButtonBody" Property="Background" Value="{StaticResource GreyGradientBrush}" />
<Setter TargetName="ButtonBody" Property="BorderBrush" Value="{StaticResource BorderBrush}" />
</Trigger>
1)
Because you can’t cast
false(bool)intoWarningLevels.Remember the first parameter of
PropertyMetadatais your default value.2) You propably will run into another problem. Your Trigger value
is a string, which can’t be converted into an enum aswell, without a type converter. The easiest way to fix that is to extend your value: