2010-10-21 4 views
2

Je n'arrive pas à mettre à jour une propriété de dépendance dans Silverlight. Je suis en train de définir une propriété et de la notifier dans ma classe parente. Le contrôle personnalisé écoute cette propriété en modifiant une propriété de dépendance, mais ne la touche jamais (appelle le rappel de modification). J'ai joué avec et la seule fois que je peux l'obtenir pour frapper est si je définis une valeur par défaut, mais il ne prend jamais la nouvelle valeur. Je suis en train de définir des points d'arrêt et de voir les valeurs changer, et même de mettre un bloc de texte avec l'objet NotificationModel.Type et ça change très bien. S'il vous plaît aider!La propriété de cohérence Silverlight ne met pas à jour/ne modifie pas

Main.xaml

<views:NotificationView x:Name="NotificationView" Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="2" Notification="{Binding Path=DataContext.Notification, ElementName=LayoutRoot, Mode=TwoWay}"></views:NotificationView> 

MainViewModel.cs

void part_NotificationChanged(object sender, NotificationChangedEventArgs e) 
    { 
     Notification = new NotificationModel() 
     { 
      Notifications = e.Notification.Notifications, 
      Type = e.Notification.Type 
     }; 
    } 

    private NotificationModel _notification; 
    public NotificationModel Notification 
    { 
     get 
     { 
      return _notification; 
     } 
     set 
     { 
      _notification = value; 
      this.OnPropertyChanged("Notification"); 
     } 
    } 

NotificationView.xaml.cs

public partial class NotificationView : UserControl 
{ 
    public NotificationView() 
    { 
     InitializeComponent(); 


    } 

    public NotificationModel Notification 
    { 
     get 
     { 
      return (NotificationModel)GetValue(NotificationProperty); 
     } 
     set 
     { 
      SetValue(NotificationProperty, value); 
     } 
    } 

    public static readonly DependencyProperty NotificationProperty = 
     DependencyProperty.Register("Notification", typeof(NotificationModel), typeof(NotificationView), new PropertyMetadata(new PropertyChangedCallback(OnNotificationChanged))); 


    private static void OnNotificationChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 
     NotificationView view = d as NotificationView; 

     if (view != null) 
     { 
      NotificationModel notification = e.NewValue as NotificationModel; 

      if (notification != null) 
      { 
       switch (notification.Type) 
       { 
        case NotificationType.Success: 
         view.LayoutRoot.Children.Add(new SuccessView() { Message = notification.Notifications.FirstOrDefault() }); 
         break; 
        case NotificationType.Error: 
         view.LayoutRoot.Children.Add(new ErrorsView() { Errors = notification.Notifications }); 
         break; 
        case NotificationType.Caution: 
         break; 
        default: 
         view.LayoutRoot.Children.Clear(); 
         break; 
       } 
      } 
     } 
    } 
} 

Répondre

1

Je ne vois rien de mal avec le code que vous avez posté jusqu'à présent. Il est supposé que OnPropertyChanged déclenche l'événement PropertyChanged de INotifyPropertyChanged implémenté par la classe conteneur. Je voudrais porter attention à la Binding elle-même. Ceux-ci échouent généralement silencieusement, si le chemin de la propriété n'est pas résolvable, il ne se passe généralement rien.

Etes-vous sûr de vouloir référencer le LayoutRoot par son nom? Le contrôle ne se trouve-t-il pas déjà à l'intérieur LayoutRoot sans que d'autres contrôles ancêtres aient leur DataContext détournés ailleurs? Pourquoi est-ce que la liaison bidirectionnelle est nécessaire, cela semble très étrange? Essayez simplement: -

<views:NotificationView x:Name="NotificationView" Notification="{Binding Notification}" ... /> 
+0

Croyez-moi, je l'ai fait aussi, il est juste à gauche sur le code que j'ai essayé d'utiliser au lieu de celui que vous venez de mentionner, mais fonctionne de la même si. Oui, il doit en quelque sorte échouer silencieusement et je ne peux tout simplement pas préciser comment et où. –