簡體   English   中英

將依賴項屬性添加到自定義控件會導致樣式出現奇怪的錯誤

[英]Adding dependency property to custom control causes strange errors to occur to style

我正在嘗試創建一個從ListBoxItem擴展的自定義控件,如下所示:

public class MainNavListBoxItem : ListBoxItem
{
    public MainNavListBoxItem()
    {
        this.DefaultStyleKey = typeof(MainNavListBoxItem);
    }
}

我有一個在ResourceDictionary中定義的樣式,如下所示:

<Style TargetType="assets:MainNavListBoxItem">
        <Setter Property="Padding" Value="3"/>
        <Setter Property="HorizontalContentAlignment" Value="Left"/>
        <Setter Property="VerticalContentAlignment" Value="Top"/>
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="TabNavigation" Value="Local"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="assets:MainNavListBoxItem">
                    <Grid Height="50">
                        <VisualStateManager.VisualStateGroups>
...

現在它編譯並運行良好,但是只要我向MainNavListBoxItem類添加一個額外的依賴屬性,我就會開始得到如下錯誤:

無法從文本“填充”創建“System.Windows.DependencyProperty”。

如果我在樣式中重新排列Setter標簽,它將始終報告最頂層的標簽。

我的依賴屬性代碼供參考:

public ImageSource ImageDark
{
    get { return (ImageSource)GetValue(ImageDarkProperty); }
    set { SetValue(ImageDarkProperty, value); }
}
public static readonly DependencyProperty ImageDarkProperty =
    DependencyProperty.Register("ImageDark", typeof(ImageSource), typeof(MainNavListBoxItem), new PropertyMetadata(0));

這里發生了什么?! 我希望能夠在樣式中使用此ImageDark依賴項屬性!

我已經對此錯誤進行了大量搜索,但似乎沒有任何內容與此問題相關。

這個問題是因為我忘了將依賴屬性中的new PropertyMetadata(0)更改為new PropertyMetadata(null) 謝謝肯特。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM