Liaison Silverlight UserControl
-
05-07-2019 - |
Question
J'ai un contrôle utilisateur simple
Xaml:
<UserControl x:Class="GraemeGorman_Controls.Navigation.NavigationItem"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Border x:Name="borderLayoutRoot">
<TextBlock x:Name="textBlockCaption" Text="{Binding Caption}" />
</Border>
</UserControl>
Cs:
namespace GraemeGorman_Controls.Navigation
{
public partial class NavigationItem : UserControl
{
public static readonly DependencyProperty CaptionProperty = DependencyProperty.Register(
"Caption",
typeof (string),
typeof (NavigationItem),
new PropertyMetadata(new PropertyChangedCallback(OnCaptionChanged)));
public string Caption
{
get {return (string)GetValue(CaptionProperty);}
set {SetValue(CaptionProperty, value);}
}
public NavigationItem()
{
InitializeComponent();
}
private static void OnCaptionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
//null
}
}
}
Quel est mon problème, lorsque je crée une instance du contrôle que la légende ne montre jamais - maintenant, j'ai testé la propriété dans la fonction OnCaptionChanged en utilisant e.NewValue et c'est la valeur correcte. Quel est le problème avec ma liaison?
Si j'écris dans le code derrière la propriété caption pour le jeu
textBlockCaption.Text = value;
Cela semble bien ...
Toute aide appréciée
Merci Graeme
La solution
D'après le code derrière, il vous manque une ligne de code.
Essayez d’ajouter DataContext = this;
dans votre constructeur. Cela a fonctionné pour moi dans le passé.
Autres conseils
Comment créez-vous l'instance du contrôle NavigationItem?
vous devrez faire quelque chose comme:
<Page ...
xmlns:gg="clr-namespace:GraemeGorman_Controls.Navigation">
<gg:NavigationItem Caption="FooBar" />
ou même
<gg:NavigationItem Caption="{Binding Path=TheCaption}" />
où TheCaption est une propriété du DataContext de votre page (votre ViewModel, par exemple)
J'espère que cela vous aidera:)