简体   繁体   English

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

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

i created a user control with some dependency properties. 我创建了具有某些依赖项属性的用户控件。 When i try to add the control to a window i get kind of a blank failure screen. 当我尝试将控件添加到窗口时,出现空白的故障屏幕。

The inner most exception says : "value was not the correct type as registered for the dp property" (i hope this is the correct Translation - found it here: https://msdn.microsoft.com/de-de/library/ms597473(v=vs.110).aspx ) 最里面的异常说:“值不是为dp属性注册的正确类型”(我希望这是正确的翻译-在此处找到它: https : //msdn.microsoft.com/de-de/library/ms597473 (v = vs.110).aspx

I would have translated it into " the Standard value type does not correspont with the type of the property "LabelColor". 我将其转换为“标准值类型与属性“ LabelColor”的类型不对应。

here is the c# code of the control: 这是控件的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));
        }
    }
}
}

and the xaml of the control: 和控件的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>

In the main window its just: 在主窗口中,它只是:

<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>

I tied to out comment the LabelColor dependency property, but then the failure occurs for RectangleBrush so i think its a problem with the Brush . 我绑了注释LabelColor依赖项属性,但是RectangleBrush失败了,所以我认为它是Brush的问题。 I double checked the properties - the Background property of a Label has the type (System.Windows.Media.) Brush . 我双重检查属性-一个标签背景属性具有类型(System.Windows.Media。) 刷涂 Maybe this is because Brush has no default value? 也许是因为Brush没有默认值? If so how do i set it? 如果是这样,我该如何设置?

I found out that removing the PropertyMetadata helps for the depedency property issues. 我发现删除PropertyMetadata有助于解决依赖属性问题。 But then i get another Exception in the constuctor with "LayoutRoot.DataContext = this;" 但是随后我在构造器中使用“ LayoutRoot.DataContext = this;”得到了另一个异常。 which is a NullReferenceException for the LayoutRoot . 这是LayoutRoot的NullReferenceException。

I created my HexButtonControl following http://blog.scottlogic.com/2012/02/06/a-simple-pattern-for-creating-re-useable-usercontrols-in-wpf-silverlight.html 我按照http://blog.scottlogic.com/2012/02/06/a-simple-pattern-for-creating-re-useable-usercontrols-in-wpf-silverlight.html创建了HexButtonControl

In your dependency property, the new PropertyMetadata() argument is the default value to the property. 在依赖项属性中, new PropertyMetadata()参数是该属性的默认值。 Your property is of the type Brush, and you're passing a string as the default value. 您的属性属于“画笔”类型,并且您要传递字符串作为默认值。 This mistake happens in other properties too. 此错误也发生在其他属性中。 Try this, or another brush you like: 试试这个,或者你喜欢的另一个刷子:

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

Edit: Sorry, had missed the last part. 编辑:对不起,错过了最后一部分。 Appears to me that you're missing the InitializeComponent(); 在我看来,您缺少InitializeComponent(); call in your constructor, before the line you set the DataContext: 在设置DataContext的行之前,在构造函数中调用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM