J'ai le problème suivant dans WPF:WPF: Liaison d'une propriété à une coutume UserControl
J'ai défini un contrôle utilisateur (test d'espace de noms) contenant une zone de texte (et plusieurs autres commandes, afficher uniquement les parties pertinentes de le XAML):
<UserControl (...)
DataContext="{Binding RelativeSource={RelativeSource Self}}"
name="Spinbox">
(...)
<StackPanel Orientation="Horizontal">
<TextBox x:Name="tbText" (...)>
<TextBox.Text>
<Binding Path="Value" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<local:ValidateValue MinVal="0" MaxVal="1" />
</Binding.ValidationRules>
<Binding.NotifyOnValidationError>true</Binding.NotifyOnValidationError>
</Binding>
</TextBox.Text>
</TextBox>
(...)
Dans le fichier principal de la fenêtre, je me sers de ce spinbox:
<Test:SpinBox x:Name="tbTestSpinbox" Value="{Binding Path=TheValue}"
MinValue="0" MaxValue="150">
<Test:SpinBox.Behavior>
<Controls:SpinBoxNormalBehavior />
</Test:SpinBox.Behavior>
</Test:SpinBox>
Dans le code derrière, je l'ai défini TheValue:
private double theValue;
public Window1()
{
InitializeComponent();
TheValue = 10;
}
public double TheValue
{
get { return theValue; }
set
{
theValue = value;
NotifyPropertyChanged("TheValue");
}
}
/// <summary>
/// Occurs when a property value changes
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
Lorsque je tente exécuter cette application, je reçois le message dans la fenêtre de sortie:
System.Windows.Data Error: 39 : BindingExpression path error: 'TheValue' property not found on 'object' ''SpinBox' (Name='tbTestSpinbox')'. BindingExpression:Path=TheValue; DataItem='SpinBox' (Name='tbTestSpinbox'); target element is 'SpinBox' (Name='tbTestSpinbox'); target property is 'Value' (type 'Double')
Et spinbox n'est pas rempli avec la valeur 10, mais la valeur par défaut 0.
Est-ce Quelqu'un at-il une idée de comment s'assurer que la valeur est correctement affichée?