UserControl:值中的依赖项属性不是为dp属性注册的正确类型
本文关键字:属性 dp 注册 类型 依赖 UserControl | 更新日期: 2023-09-27 17:59:57
我创建了一个具有一些依赖属性的用户控件。当我试图将控件添加到窗口时,我会得到一个空白的失败屏幕。
最内部的异常是:"value不是为dp属性注册的正确类型"(我希望这是正确的翻译-在这里找到它: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 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发生了故障,所以我认为这是Brush的问题。我仔细检查了属性-标签的Background属性的类型为(System.Windows.Media.)Brush。也许这是因为Brush没有默认值?如果是,我该如何设置?
我发现删除PropertyMetadata有助于解决dependency属性问题。但后来我在构造函数中得到了另一个异常,它是"LayoutRoot.DataContext=this;",这是LayoutRoot的NullReferenceException。
我创建了我的HexButtonControlhttp://blog.scottlogic.com/2012/02/06/a-simple-pattern-for-creating-re-useable-usercontrols-in-wpf-silverlight.html
在依赖属性中,new PropertyMetadata()
参数是该属性的默认值。您的属性属于Brush类型,并且您将传递一个字符串作为默认值。这种错误也发生在其他属性中。试试这个,或者另一个你喜欢的刷子:
public static readonly DependencyProperty LabelColorProperty =
DependencyProperty.Register("LabelColor", typeof(Brush),
typeof(HexButtonControl), new PropertyMetadata(Brushes.Black));
编辑:对不起,错过了最后一部分。在我看来,在您设置DataContext:的行之前,您的构造函数中缺少了InitializeComponent();
调用
public HexButtonControl()
{
InitializeComponent();
LayoutRoot.DataContext = this;
}