Silverlight依赖项属性未更新/更改

时间:2010-10-21 05:50:00

标签: silverlight dependency-properties

我无法在Silverlight中更新依赖项属性。我正在设置一个属性,并在我的父类中通知它已更改。自定义控件通过依赖项属性侦听此属性,但它从不命中它(调用更改回调)。我一直在玩它,唯一一次我可以得到它是如果我设置一个默认值,但它永远不会采取新值。我正在设置断点并看到值发生变化,甚至将带有NotificationModel.Type对象的文本块放到它上面并且它变化很好。请帮忙!

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;
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

到目前为止,我发现您发布的代码没有任何问题。假设OnPropertyChanged触发了由包含类实现的PropertyChanged INotifyPropertyChanged事件。

我的注意力集中在Binding本身。这些通常无声地失败,如果属性路径不可解析,通常不会发生任何事情。

您确定需要按名称引用LayoutRoot吗?如果没有其他祖先控件将LayoutRoot转移到别处,控件是否已经位于DataContext内?为什么需要双向绑定,这看起来很奇怪?试着简单地说: -

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