代码之家  ›  专栏  ›  技术社区  ›  MojoFilter

Silverlight:如何接收继承的DependencyProperty中更改的通知

  •  12
  • MojoFilter  · 技术社区  · 17 年前

    我有一个继承自(你猜对了)控件的控件。 每当 FontSize Style 属性已更改。在WPF,我会打电话给 DependencyProperty.OverrideMetadata() . 当然,像这样有用的东西在Silverlight中没有位置。那么,如何接收这些通知呢?

    5 回复  |  直到 11 年前
        1
  •  16
  •   Hossein Narimani Rad    11 年前

    我觉得这是个更好的方法。仍然需要看到正反两面。

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

    现在,您可以调用register for notification来注册元素属性的更改通知,比如。

    RegisterForNotification("Text", this.txtMain,(d,e)=>MessageBox.Show("Text changed"));
    RegisterForNotification("Value", this.sliderMain, (d, e) => MessageBox.Show("Value changed"));
    

    在这里看到我的帖子 http://amazedsaint.blogspot.com/2009/12/silverlight-listening-to-dependency.html

    使用Silverlight 4.0测试版。

        2
  •  6
  •   toshok    16 年前

    这是一个相当恶心的黑客,但你可以使用双向绑定来模拟这个。

    也就是说,有类似的:

    public class FontSizeListener {
        public double FontSize {
            get { return fontSize; }
            set { fontSize = value; OnFontSizeChanged (this, EventArgs.Empty); }
        }
    
        public event EventHandler FontSizeChanged;
    
        void OnFontSizeChanged (object sender, EventArgs e) {
          if (FontSizeChanged != null) FontSizeChanged (sender, e);
        }
    }
    

    然后创建绑定,如下所示:

    <Canvas>
      <Canvas.Resources>
         <FontSizeListener x:Key="listener" />
      </Canvas.Resources>
    
      <MyControlSubclass FontSize="{Binding Mode=TwoWay, Source={StaticResource listener}, Path=FontSize}" />
    </Canvas>
    

    然后连接到控件子类中的侦听器事件。

        3
  •  0
  •   markti    17 年前

    不能从外部侦听依赖项属性更改通知。

    可以使用以下代码行访问依赖属性元数据:

    PropertyMetadata metaData = Control.ActualHeightProperty.GetMetadata(typeof(Control));
    

    但是,唯一公开的公共成员是“defaultvalue”。

    在WPF中有很多方法可以做到这一点。但目前Silverlight2或3不支持它们。

        4
  •  0
  •   Vlad Filyakov    17 年前

    我看到的唯一解决方案是听取布局更新事件-是的,我知道它被称为很多。但请注意,在某些情况下,即使fontsize或style已更改,也不会调用它。

        5
  •  -2
  •   Ana Betts    17 年前

    这是我一直使用的(虽然还没有在SL上测试过,只是在WPF上测试过):

        /// <summary>
        /// This method registers a callback on a dependency object to be called
        /// when the value of the DP changes.
        /// </summary>
        /// <param name="owner">The owning object.</param>
        /// <param name="property">The DependencyProperty to watch.</param>
        /// <param name="handler">The action to call out when the DP changes.</param>
        public static void RegisterDepPropCallback(object owner, DependencyProperty property, EventHandler handler)
        {
            // FIXME: We could implement this as an extension, but let's not get
            // too Ruby-like
            var dpd = DependencyPropertyDescriptor.FromProperty(property, owner.GetType());
            dpd.AddValueChanged(owner, handler);
        }