我有Style一個自定義控制元件,我試圖在其中設定一個附加屬性Label。
樣式
<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>
附加屬性
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);
}
}
列舉
public enum DateSelectionType
{
NotSelected,
Selected,
StartDate,
EndDate
}
標簽
<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>
錯誤
值不能為空。(引數“屬性”)
當我從Setter一切正常作業中洗掉附加屬性時。
有人可以解釋為什么我不能用這個設定Style Setter嗎?
uj5u.com熱心網友回復:
您定義了一個依賴屬性SelectionTypeProperty,因此它的名稱必須是SelectionType,您可以在檔案中閱讀:如何實作依賴屬性 (WPF .NET)
識別符號欄位必須遵循命名約定
<property name>Property。例如,如果您使用 name 注冊依賴屬性Location,則識別符號欄位應命名為LocationProperty。
但是,在您的定義中,您傳遞DateSelectionType了 name,它是屬性型別的名稱,而不是屬性本身的名稱。
如果您未能遵循此命名模式,則 WPF 設計器可能無法正確報告您的屬性,并且屬性系統樣式應用程式的某些方面可能無法按預期運行。
將名稱更改為SelectionType,它按預期作業。
public static readonly DependencyProperty SelectionTypeProperty = DependencyProperty.RegisterAttached(
"SelectionType", typeof(DateSelectionType), typeof(DateRangePickerHelper),
new PropertyMetadata(DateSelectionType.NotSelected));
如果您曾經實作具有屬性包裝器而不是方法的常規依賴屬性,則可以使用它nameof(<YourProperty>)來防止此問題和重命名問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/506348.html
上一篇:同時運行多個影片
下一篇:使用全選過濾控制元件
