J'ai créé un contrôle utilisateur avec certaines propriétés de dépendance. Lorsque j'essaie d'ajouter le contrôle à une fenêtre, je reçois une sorte d'écran d'échec vide.Propriétés de dépendance dans un contrôle UserControl: valeur n'était pas le type correct enregistré pour la propriété dp
La plus exception interne dit: « valeur n'a pas été le type correct comme enregistré pour la propriété dp » (J'espère que cela est la bonne traduction - trouvé ici: https://msdn.microsoft.com/de-de/library/ms597473(v=vs.110).aspx)
Je l'ai traduit en " le type de valeur standard ne correspont pas avec le type de la propriété "LabelColor"
est ici le code C# du contrôle:.
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));
}
}
}
}
et XAML du contrôle:
<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>
Dans la fenêtre principale est juste:
<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>
Je lié à des commentaires du LabelColor de propriété de dépendance, mais l'échec se produit pour RectangleBrush donc je pense que un problème avec le Brush. J'ai vérifié les propriétés - la propriété propriété d'un étiquette a le type (System.Windows.Media.) Brush. Peut-être parce que Brush n'a pas de valeur par défaut? Si oui, comment puis-je le définir?
J'ai découvert que la suppression du PropertyMetadata aide pour les problèmes de propriété de la propriété. Mais alors je reçois une autre exception dans le constuctor avec "LayoutRoot.DataContext = this;" qui est une exception NullReferenceException pour le LayoutRoot.
J'ai créé mon HexButtonControl suivant http://blog.scottlogic.com/2012/02/06/a-simple-pattern-for-creating-re-useable-usercontrols-in-wpf-silverlight.html
vous avez raison :). Le InitializeComponent() était manquant. OMG je me sens comme la personne la plus stupide dans l'histoire de codage ... :( – Thoms
@Thoms Il arrive que nous ayons besoin d'une nouvelle paire d'yeux (est-ce une expression en anglais?) Pour découvrir les erreurs que nous ne voyons pas. – Magnetron