[英]Unable to set Attached Property in a Setter
I have a Style
in a custom control, in which I am trying to set an attached property on a Label
.我在自定义控件中有一个
Style
,我试图在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
<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.
值不能是 null。 (Parameter 'property')
(参数“属性”)
When I remove the attached property from the Setter
everything works correctly.当我从
Setter
中删除附加属性时,一切正常。
Can someone explain why I cannot set this with the Style
Setter
?有人可以解释为什么我不能用
Style
Setter
它吗?
You define a dependency property SelectionTypeProperty
, so its name must be SelectionType
, as your can read in the documentation: How to implement a dependency property (WPF .NET)您定义了一个依赖属性
SelectionTypeProperty
,因此它的名称必须是SelectionType
,您可以在文档中阅读: 如何实现依赖属性 (WPF .NET)
The identifier field must follow the naming convention
<property name>Property
.标识符字段必须遵循命名约定
<property name>Property
。 For instance, if you register a dependency property with the nameLocation
, then the identifier field should be namedLocationProperty
.例如,如果您使用名称
Location
注册依赖属性,则标识符字段应命名为LocationProperty
。
However, in your definition, you pass DateSelectionType
name, which is the name of the type of the property , but not the name of the property itself.但是,在您的定义中,您传递
DateSelectionType
名称,它是属性类型的名称,而不是属性本身的名称。
If you fail to follow this naming pattern, then WPF designers might not report your property correctly, and aspects of the property system style application might not behave as expected .
如果您未能遵循此命名模式,则 WPF 设计师可能无法正确报告您的属性,并且属性系统样式应用程序的某些方面可能无法按预期运行。
Change the name to SelectionType
and it works as expected.将名称更改为
SelectionType
并按预期工作。
public static readonly DependencyProperty SelectionTypeProperty = DependencyProperty.RegisterAttached(
"SelectionType", typeof(DateSelectionType), typeof(DateRangePickerHelper),
new PropertyMetadata(DateSelectionType.NotSelected));
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.如果您曾经实现具有属性包装器而不是方法的常规依赖属性,则可以使用
nameof(<YourProperty>)
来防止此问题和重命名问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.