2016-03-25 13 views
2

Ich brauche eine benutzerdefinierte angefügte Eigenschaft zu erstellen, die leer zu sein in der Lage, wie unten dargestellt:WPF Benutzerdefinierte angebracht Eigenschaft, die leer sein

<Window x:Class="WpfApplication1.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:l="clr-namespace:WpfApplication1" 
    Title="MainWindow" Height="350" Width="525"> 
<Grid> 
    <l:SimpleAttach.MyProperty></l:SimpleAttach.MyProperty> 

</Grid> 

Ich habe versucht, es auf diese Weise tun:

public static class SimpleAttach 
{ 
    public static object GetMyProperty(DependencyObject obj) 
    { 
     return obj.GetValue(MyPropertyProperty); 
    } 

    public static void SetMyProperty(DependencyObject obj, object value) 
    { 
     obj.SetValue(MyPropertyProperty, value); 
    } 

    // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty MyPropertyProperty = 
     DependencyProperty.RegisterAttached("MyProperty", typeof(object), typeof(SimpleAttach), new PropertyMetadata(null)); 
} 

Aber das gibt nur einen Fehler:

The property "MyProperty" cannot be empty.

Im Gegensatz dazu scheint MS kein Problem beim Erstellen leerer angehängter Eigenschaften zu haben. Hier ist ein Beispiel, das keinen Fehler geben über leer ist:

<TextBlock Name="VatAmount" Text="hello world" TextAlignment="Right" Margin="0,0,20,111" HorizontalAlignment="Right" Width="120" Height="16" VerticalAlignment="Bottom"> 
     <i:Interaction.Behaviors> 
     </i:Interaction.Behaviors> 
    </TextBlock> 

Also, was muss ich tun XAML sagen, dass diese Eigenschaft darf leer sein?

+0

meinst du ' null ' –

+0

Ich meine, dass zwischen den Anfangs- und Endtags nichts sein sollte. Ich würde erwarten, dass der tatsächliche Wert daher null ist. Betrachten wir das Beispiel von Interaction.Behaviours, ich denke, es ist eine Sammlung und ermöglicht daher, dass es 0 oder n Elemente gibt. – dayneo

+0

Try Set-Eigenschaft zu

Antwort

1

Okay, also habe ich endlich meine Probleme herausgefunden. Manchmal hilft es, die offizielle Dokumentation zu lesen: XAML Syntax In Detail

Insbesondere schien ich auf "Inhaltseigenschaften" zu verweisen.

XAML content syntax is a syntax that is only enabled on classes that specify the ContentPropertyAttribute as part of their class declaration. The ContentPropertyAttribute references the property name that is the content property for that type of element (including derived classes). When processed by a XAML processor, any child elements or inner text that are found between the opening and closing tags of the object element will be assigned to be the value of the XAML content property for that object.

Zweitens, im Beispiel von Microsofts Interactivity.Behaviors, das ist eine Sammlung und Sammlungen können in ihnen 0 Produkte haben.

Außerdem, wenn Sie die Straße nach unten gehen von einem angeschlossenen Verhalten implementieren, die eine Sammlung ist, dann ist dieser Beitrag wird Sie durch die Macken führen: Attached property of type list