UserControl 中的依赖属性:值不是为 dp 注册的正确类型 属性

Dependency Properties in a UserControl : value was not the correct type as registered for the dp property

我创建了一个具有一些依赖属性的用户控件。当我尝试将控件添加到 window 时,我得到了一个空白的失败屏幕。

最里面的异常说:"value was not the correct type as registered for the dp property"(我希望这是正确的翻译 - 在这里找到它:https://msdn.microsoft.com/de-de/library/ms597473(v=vs.110).aspx

我会把它翻译成“标准值类型与 属性 "LabelColor" 的类型不对应。

这里是控件的c#代码:

namespace HexButton
{
public partial class HexButtonControl : UserControl
{

    #region Dependency Properties

    #region LabelText

    /// <summary>
    /// Gets or sets the LabelText which is displayed next to the (unit-)rectangle
    /// </summary>
    public string LabelText
    {
        get { return (string)GetValue(LabelTextProperty); }
        set { SetValue(LabelTextProperty, value); }
    }

    /// <summary>
    /// Identified the LabelText dependency property
    /// </summary>
    public static readonly DependencyProperty LabelTextProperty =
        DependencyProperty.Register("LabelText", typeof(string),
          typeof(HexButtonControl), new PropertyMetadata(""));

    #endregion
    #region LabelColor

    /// <summary>
    /// Gets or sets the LabelColor (background) which is displayed next to the (unit-)rectangle
    /// </summary>
    public Brush LabelColor
    {
        get { return (Brush)GetValue(LabelColorProperty); }
        set { SetValue(LabelColorProperty, value); }
    }

    /// <summary>
    /// Identified the LabelColor dependency property
    /// </summary>
    public static readonly DependencyProperty LabelColorProperty =
        DependencyProperty.Register("LabelColor", typeof(Brush),
          typeof(HexButtonControl), new PropertyMetadata(""));

    #endregion

    #region RectangleBrush

    /// <summary>
    /// Gets or sets the Brush which is used to fill the (unit-)rectangle within the hexagon
    /// </summary>
    public Brush RectangleBrush
    {
        get { return (Brush)GetValue(RectangleBrushProperty); }
        set { SetValue(RectangleBrushProperty, value); }
    }

    /// <summary>
    /// Identified the RectangleBrush dependency property
    /// </summary>
    public static readonly DependencyProperty RectangleBrushProperty =
        DependencyProperty.Register("RectangleBrush", typeof(Brush),
          typeof(HexButtonControl), new PropertyMetadata(""));

    #endregion

    #region HexBackground

    /// <summary>
    /// Gets or sets the Brush which is used to fill the background of the hexagon
    /// </summary>
    public Brush HexBackground
    {
        get { return (Brush)GetValue(HexBackgroundProperty); }
        set { SetValue(HexBackgroundProperty, value); }
    }

    /// <summary>
    /// Identified the HexBackground dependency property
    /// </summary>
    public static readonly DependencyProperty HexBackgroundProperty =
        DependencyProperty.Register("HexBackground", typeof(Brush),
          typeof(HexButtonControl), new PropertyMetadata(""));

    #endregion
    #region HexBorderColor

    /// <summary>
    /// Gets or sets the Brush which is used to draw the border of the hexagon
    /// </summary>
    public Brush HexBorderColor
    {
        get { return (Brush)GetValue(HexBorderColorProperty); }
        set { SetValue(HexBorderColorProperty, value); }
    }

    /// <summary>
    /// Identified the HexBorderColor dependency property
    /// </summary>
    public static readonly DependencyProperty HexBorderColorProperty =
        DependencyProperty.Register("HexBorderColor", typeof(Brush),
          typeof(HexButtonControl), new PropertyMetadata(""));

    #endregion              
    #region HexStokeDashArray

    /// <summary>
    /// Gets or sets the the StrokeDashArray for the border of the Hhxagon
    /// </summary>
    public DoubleCollection HexStokeDashArray
    {
        get { return (DoubleCollection)GetValue(HexStokeDashArrayProperty); }
        set { SetValue(HexStokeDashArrayProperty, value); }
    }

    /// <summary>
    /// Identified the HexStokeDashArray dependency property
    /// </summary>
    public static readonly DependencyProperty HexStokeDashArrayProperty =
        DependencyProperty.Register("HexStokeDashArray", typeof(DoubleCollection),
          typeof(HexButtonControl), new PropertyMetadata(""));

    #endregion

    #endregion

    public HexButtonControl()
    {            
        LayoutRoot.DataContext = this;
    }
}

public class HexModelObject
{
    private string _labelText;
    public string LabelText
    {
        get { return _labelText; }
        set
        {
            _labelText = value;
            OnPropertyChanged("LabelText");
        }
    }

    private Brush _labelColor;
    public Brush LabelColor
    {
        get { return _labelColor; }
        set
        {
            _labelColor = value;
            OnPropertyChanged("LabelColor");
        }
    }

    private Brush _rectangleBrush;
    public Brush RectangleBrush
    {
        get { return _rectangleBrush; }
        set
        {
            _rectangleBrush = value;
            OnPropertyChanged("RectangleBrush");
        }
    }

    private Brush _hexBackground;
    public Brush HexBackground
    {
        get { return _hexBackground; }
        set
        {
            _hexBackground = value;
            OnPropertyChanged("HexBackground");
        }
    }

    private Brush _hexBorderColor;
    public Brush HexBorderColor
    {
        get { return _hexBorderColor; }
        set
        {
            _hexBorderColor = value;
            OnPropertyChanged("HexBorderColor");
        }
    }

    private DoubleCollection _hexStrokeDashArray;
    public DoubleCollection HexStrokeDashArray
    {
        get { return _hexStrokeDashArray; }
        set
        {
            _hexStrokeDashArray = value;
            OnPropertyChanged("HexStrokeDashArray");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
}

和控件的xaml:

<UserControl x:Class="HexButton.HexButtonControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         Height="91" Width="104">
         <!--d:DesignHeight="91" d:DesignWidth="104">-->
<Canvas x:Name="LayoutRoot">
    <Polygon Points="27,2 77,2 102,45 77,89 27,89 2,45"
             StrokeThickness="4"
             Fill="{Binding Path=HexBackground}"
             Stroke="{Binding Path=HexBorderColor}"
             StrokeDashArray="{Binding Path=HexStokeDashArray}"/>
    <Rectangle
             Height="70"
             Width="48"
             Fill="{Binding Path=RectangleBrush}"
             Canvas.Left="28"
             Canvas.Top="10"
     />
    <Label 
        Height="24" 
        Width="14"
        Padding="0"
        FontSize="18"
        FontWeight="Bold"            
        Background="{Binding Path=LabelColor}"
        Canvas.Left="80" 
        Canvas.Top="31"
        Content="{Binding Path=LabelText}" />        
</Canvas>

主要window它只是:

<Window x:Class="HexButton.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525"
    xmlns:myControls="clr-namespace:HexButton">
<Grid Name="myGrid">
    <myControls:HexButtonControl x:Name="UC1"
        HexBackground="AliceBlue" HexBorderColor="Black" RectangleBrush="Green" LabelColor="Beige" LabelText="asdf">
    </myControls:HexButtonControl>
</Grid>
</Window>

我对 LabelColor 依赖项 属性 进行了评论,但随后 RectangleBrush 发生了故障,所以我认为它画笔 有问题。我仔细检查了属性 - Background 属性 的 Label 具有类型 (System.Windows.Media.)画笔。也许这是因为Brush没有默认值?如果可以,我该如何设置?

我发现删除 PropertyMetadata 有助于解决 depedency 属性 问题。但是后来我在构造函数中得到另一个异常 "LayoutRoot.DataContext = this;" 这是 LayoutRoot 的 NullReferenceException。

我在 http://blog.scottlogic.com/2012/02/06/a-simple-pattern-for-creating-re-useable-usercontrols-in-wpf-silverlight.html

之后创建了 HexButtonControl

在您的依赖项 属性 中,new PropertyMetadata() 参数是 属性 的默认值。您的 属性 是 Brush 类型,并且您正在传递一个字符串作为默认值。这个错误也发生在其他属性中。试试这个,或者你喜欢的其他画笔:

public static readonly DependencyProperty LabelColorProperty =
    DependencyProperty.Register("LabelColor", typeof(Brush),
      typeof(HexButtonControl), new PropertyMetadata(Brushes.Black));

编辑: 对不起,错过了最后一部分。在我看来,您在构造函数中缺少 InitializeComponent(); 调用,在您设置 DataContext 的行之前:

public HexButtonControl()
{   
    InitializeComponent();
    LayoutRoot.DataContext = this;
}