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