从另一个OnChanged事件设置一个DependencyProperty的值

时间:2012-03-07 09:05:21

标签: silverlight xaml custom-controls dependency-properties

我已经定义了两个DependencyProperties:

public int TempProp1
        {
            get { return (int)GetValue(TempProp1Property); }
            set { SetValue(TempProp1Property, value); }
        }

        // Using a DependencyProperty as the backing store for TempProp1.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TempProp1Property =
            DependencyProperty.Register("TempProp1", typeof(int), typeof(CustomControl1), new PropertyMetadata(200));
第二个:

public int TempProp2
        {
            get { return (int)GetValue(TempProp2Property); }
            set { SetValue(TempProp2Property, value); }
        }

        // Using a DependencyProperty as the backing store for TempProp2.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TempProp2Property =
            DependencyProperty.Register("TempProp2", typeof(int), typeof(CustomControl1), new PropertyMetadata(10, OnTempProp2Changed));


        private static void OnTempProp2Changed(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            CustomControl1 cc = o as CustomControl1;
            if (cc == null) return;

            cc.TempProp1 = (((int)(e.NewValue)) / 2);
        }

请注意,在 OnTempProp2Changed()中,我在 TempProp1 属性上设置了值。

问题: TempProp1 值未反映到XAML。

我想实现 TempProp1 到XAML的序列化,即使它的值是从另一个DependencyProperty( TempProp2 )设置的。 这是在Silverlight自定义控件中完成的,并且这两个DependencyProperties没有绑定或任何其他特定的东西。

0 个答案:

没有答案