I have a Style in a custom control, in which I am trying to set an attached property on a Label.
The Style
<Style x:Key="DayNumberStyle" TargetType="Label" BasedOn="{StaticResource {x:Type Label}}">
<Setter Property="HorizontalAlignment" Value="Center"></Setter>
<Setter Property="local:DateRangePickerHelper.SelectionType" Value="Selected"></Setter>
</Style>
The Attached Property
public class DateRangePickerHelper : FrameworkElement
{
public static readonly DependencyProperty SelectionTypeProperty = DependencyProperty.RegisterAttached(
"DateSelectionType", typeof(DateSelectionType), typeof(DateRangePickerHelper),
new PropertyMetadata(DateSelectionType.NotSelected));
public static void SetSelectionType(DependencyObject element, DateSelectionType value)
{
element.SetValue(SelectionTypeProperty, value);
}
public static DateSelectionType GetSelectionType(DependencyObject element)
{
return (DateSelectionType)element.GetValue(SelectionTypeProperty);
}
}
The Enum
public enum DateSelectionType
{
NotSelected,
Selected,
StartDate,
EndDate
}
The Label
<Label Style="{StaticResource DayNumberStyle}" Grid.Column="0" Content="{Binding Sunday}">
<b:Interaction.Triggers>
<b:EventTrigger EventName="MouseLeftButtonUp">
<b:InvokeCommandAction Command="{Binding Path=LabelClickedCommand, RelativeSource={RelativeSource AncestorType=local:DateRangePicker}}" CommandParameter="{Binding Sunday}"></b:InvokeCommandAction>
</b:EventTrigger>
</b:Interaction.Triggers>
</Label>
The Error
Value cannot be null. (Parameter 'property')
When I remove the attached property from the Setter everything works correctly.
Can someone explain why I cannot set this with the Style Setter?
You define a dependency property
SelectionTypeProperty, so its name must beSelectionType, as your can read in the documentation: How to implement a dependency property (WPF .NET)However, in your definition, you pass
DateSelectionTypename, which is the name of the type of the property, but not the name of the property itself.Change the name to
SelectionTypeand it works as expected.In case you ever implement a regular dependency property that has property wrappers instead of methods, you could use
nameof(<YourProperty>)to prevent this and renaming issues.