开发者

Silverlight Depedency Property Not Updating/Changing

开发者 https://www.devze.com 2023-01-20 13:02 出处:网络
I am having trouble updating a dependency property in Silverlight.I am setting a property and notifying it changed in my parent class.The custom control listens to this property changing through a dep

I am having trouble updating a dependency property in Silverlight. I am setting a property and notifying it changed in my parent class. The custom control listens to this property changing through a dependency property, but it never hits it (calls the change callback). I have been playing around with it and the only time I can get it to hit is if I set a default value, but it never takes on the new value. I am setting breakpoints and seeing the values changing, and even put a textblock with the NotificationModel.Type object to it and it changes fine. Please help!

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(Noti开发者_运维技巧ficationProperty); 
        }
        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;
                }
            }
        }
    }
}


I can see nothing wrong with the code you have posted so far. There is an assumption that OnPropertyChanged is firing the PropertyChanged event of INotifyPropertyChanged implemented by the containing class.

My attention would be on the Binding itself. These usually fail silently, if the property path is not resolvable nothing usually happens.

Are you sure you need to reference the LayoutRoot by name? Does the control not already sit inside LayoutRoot without other ancestor controls having their DataContext diverted elsewhere? Why is two way binding needed, that seems highly odd? Try simply:-

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

精彩评论

暂无评论...
验证码 换一张
取 消