2009-06-18 6 views
6

J'essaie de créer un UserControl réutilisable dans WPF qui a un Label et un TextBox. Je souhaite ajouter des propriétés à mon UserControl pour faire passer les champs Text des deux contrôles enfants jusqu'au parent pour faciliter la liaison. J'ai lu que j'ai besoin d'un peu de hocus pocus en ajoutant des propriétaires à DependencyProperties. Voici mon code maintenant. Il semble proche mais pas tout à fait raison. Des idées?Contrôles composites WPF

Voici le XAML:

<UserControl x:Class="MAAD.AircraftExit.Visual.LabelTextBox" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Height="20" Width="300"> 
    <DockPanel> 
     <TextBlock Text="{Binding Path=Label, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" DockPanel.Dock="Left" TextAlignment="Right" Width="122" /> 
     <TextBlock Text=": " DockPanel.Dock="Left"/> 
     <TextBox Text="{Binding Path=Text, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" /> 
    </DockPanel> 
</UserControl> 

Et le code derrière:

public partial class LabelTextBox : UserControl 
{ 
    public static readonly DependencyProperty LabelProperty = DependencyProperty.Register("Label", typeof(string), typeof(LabelTextBox)); 
    public string Label 
    { 
     get { return (string)GetValue(LabelProperty); } 
     set { SetValue(LabelProperty, value); } 
    } 

    public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(LabelTextBox)); 
    public string Text 
    { 
     get { return (string)GetValue(TextProperty); } 
     set { SetValue(LabelTextBox.TextProperty, value); } 
    } 

    public LabelTextBox() 
    { 
     InitializeComponent(); 

     ClearValue(HeightProperty); 
     ClearValue(WidthProperty); 
    } 
} 

Edit: Voici le code de travail final. Je suis passé à la liaison de source relative.

+1

J'ai essayé la même chose au premier abord en supposant que c'est ce que le concept du propriétaire était. Malheureusement, j'ai rencontré les mêmes problèmes et j'ai fini par recourir à la reliure. Mes propriétés non dépendantes ont également une solution personnalisée différente pour la propagation des notifications de modification de propriété. – jpierson

Répondre

6

La liaison est vraiment la voie à suivre:

XAML:

<UserControl x:Class="testapp.LabelTextBox " 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
Height="300" Width="300" x:Name="This"> 
<DockPanel> 
    <TextBlock DockPanel.Dock="Left" TextAlignment="Right" Width="70" Name="label" Text="{Binding Label, ElementName=This}" /> 
    <TextBlock Text=": " DockPanel.Dock="Left" /> 
    <TextBox Name="textBox" Text="{Binding Text, ElementName=This}" /> 
</DockPanel> 

code Derrière:

public partial class LabelTextBox : UserControl 
{ 
    public LabelTextBox() 
    { 
     InitializeComponent(); 
     Label = "Label"; 
     Text = "Text"; 
    } 
    public static readonly DependencyProperty LabelProperty = DependencyProperty.Register("Label", typeof(string), typeof(LabelTextBox), new FrameworkPropertyMetadata(LabelPropertyChangedCallback)); 
    private static void LabelPropertyChangedCallback(DependencyObject controlInstance, DependencyPropertyChangedEventArgs args) 
    { 
    } 
    public string Label 
    { 
     get { return (string) GetValue(LabelProperty); } 
     set { SetValue(LabelProperty, value); } 
    } 

    public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(LabelTextBox), new FrameworkPropertyMetadata(TextPropertyChangedCallback)); 
    private static void TextPropertyChangedCallback(DependencyObject controlInstance, DependencyPropertyChangedEventArgs args) 
    { 
    } 
    public string Text 
    { 
     get { return (string) GetValue(TextProperty); } 
     set { SetValue(LabelTextBox.TextProperty, value); } 
    } 
} 
+0

Merci pour le code, j'obtiens cette sortie quand j'essaie de l'utiliser: System.Windows.Data Erreur: 4: Impossible de trouver la source pour la liaison avec la référence 'ElementName = This'. BindingExpression: Path = Label; DataItem = null; l'élément cible est 'TextBlock' (Name = ''); La propriété target est 'Text' (type 'String') –

+0

Désolé, je l'ai compris. Vous avez défini ceci comme le nom du contrôle. Merci!! –

1

Je n'ai pas examiné exactement pourquoi votre implémentation ne fonctionne pas, mais je ne comprends pas vraiment pourquoi vous le faites de cette façon. Pourquoi ne pas simplement définir les propriétés de dépendance dont vous avez besoin dans UserControl, puis vous lier à ces propriétés?

public static readonly DependencyProperty LabelTextProperty = ...; 

Et puis dans votre XAML:

<Label Content="{Binding LabelText}"/> 
Questions connexes