2016-08-02 36 views
0

Ich habe ein Benutzersteuerelement mit einigen Abhängigkeitseigenschaften erstellt. Wenn ich versuche, das Steuerelement zu einem Fenster hinzuzufügen, erhalte ich einen leeren Fehlerbildschirm.Abhängigkeitseigenschaften in einem UserControl: Wert war nicht der richtige Typ, wie für die DP-Eigenschaft registriert

Die innerste Ausnahme, sagt: „Wert ist nicht der richtige Typ wie für die dp Eigenschaft registriert“ (Ich hoffe, das ist die korrekte Übersetzung - es findet sie hier: https://msdn.microsoft.com/de-de/library/ms597473(v=vs.110).aspx)

ich es in übersetzt hätte " der Standardwert Typ mit dem Typ der Eigenschaft „labelcolor“ nicht correspont

hier ist die C# -Code die Steuerung:.

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)); 
     } 
    } 
} 
} 

und die xAML der Steuerung:

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

Im Hauptfenster sein gerade:

<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 out Kommentar der labelcolor Abhängigkeitseigenschaft gebunden, aber dann tritt der Fehler für RectangleBrush so denke ich, es ist ein Problem mit der Bürste. Ich habe die Eigenschaften überprüft - die Hintergrund Eigenschaft eines Label hat den Typ (System.Windows.Media.) Pinsel. Vielleicht liegt das daran, Pinsel hat keinen Standardwert? Wenn ja, wie stelle ich es ein?

Ich fand heraus, dass das Entfernen der PropertyMetadata hilft für die Abhängigkeit Eigenschaftsprobleme. Aber dann bekomme ich eine weitere Exception im constructor mit "LayoutRoot.DataContext = this;" Dies ist eine NullReferenceException für LayoutRoot. Folgenden http://blog.scottlogic.com/2012/02/06/a-simple-pattern-for-creating-re-useable-usercontrols-in-wpf-silverlight.html

Antwort

0

In Ihrer Abhängigkeitseigenschaft

ich meine HexButtonControl erstellt, ist das new PropertyMetadata() Argument der Standardwert der Eigenschaft. Ihre Eigenschaft ist vom Typ "Pinsel" und Sie übergeben eine Zeichenfolge als Standardwert. Dieser Fehler tritt auch in anderen Eigenschaften auf. Versuchen Sie, diese oder eine andere Bürste Sie mögen:

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

Edit: Sorry, hatte den letzten Teil verpasst. Scheint mir, dass Sie das InitializeComponent(); Anruf in Ihrem Konstruktor fehlt, vor der Zeile Sie die Datacontext gesetzt:

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

Sie rechts :) sind. Die InitializeComponent() fehlte. OMG Ich fühle mich wie die dümmste Person in der Geschichte Geschichte ... :( – Thoms

+0

@Thoms Es passiert. Manchmal brauchen wir ein neues Paar Augen (ist das ein Ausdruck in Englisch?), Um die Fehler zu finden, die wir nicht sehen. – Magnetron