1. 程式人生 > >silverlight屬性改變事件通知

silverlight屬性改變事件通知

void property att lap str ring nbsp callback ebo

工作中遇到silverlight本身沒有提供的某些屬性改變事件,但又需要在屬性改變時得到通知,Google搬運stack overflow,原地址

技術分享
 /// Listen for change of the dependency property
    public void RegisterForNotification(string propertyName, FrameworkElement element, PropertyChangedCallback callback)
    {

        //Bind to a depedency property
        Binding b = new
Binding(propertyName) { Source = element }; var prop = System.Windows.DependencyProperty.RegisterAttached( "ListenAttached"+propertyName, typeof(object), typeof(UserControl), new System.Windows.PropertyMetadata(callback)); element.SetBinding(prop, b); }
View Code 技術分享
RegisterForNotification("Text", this.txtMain,(d,e)=>MessageBox.Show("Text changed"));
RegisterForNotification("Value", this.sliderMain, (d, e) => MessageBox.Show("Value changed"));
View Code

silverlight屬性改變事件通知