依赖属性问题

时间:2011-03-04 19:36:31

标签: silverlight-4.0 dependency-properties

我使用websites代码作为参考,但我想要做的是不行。支持属性永远不会改变......我不知道我期待发生的事情是错误的。

public class QuestionTemplateSelector : UserControl
    {
        public DataTemplate TemplateString { get; set; }
        public DataTemplate TemplateBoolean { get; set; }
        public DataTemplate TemplateSingleMultipleChoice { get; set; }
        public DataTemplate TemplateAnyMultipleChoice { get; set; }


        /// <summary>
        /// The <see cref="QuestionType" /> dependency property's name.
        /// </summary>
        public const string QuestionTypePropertyName = "QuestionType";

        /// <summary>
        /// Gets or sets the value of the <see cref="QuestionType" />
        /// property. This is a dependency property.
        /// </summary>
        public string QuestionType
        {
            get
            {
                return (string)GetValue(QuestionTypeProperty);
            }
            set
            {
                SetValue(QuestionTypeProperty, value);
            }
        }

        /// <summary>
        /// Identifies the <see cref="QuestionType" /> dependency property.
        /// </summary>
        public static readonly DependencyProperty QuestionTypeProperty = DependencyProperty.Register(
            QuestionTypePropertyName,
            typeof(string),
            typeof(QuestionTemplateSelector), new PropertyMetadata(QuestionTypeChangedCallBack));

        private static void QuestionTypeChangedCallBack(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            Debug.WriteLine(string.Format("Old Value: {1}{0}New Value: {2}", " - ", e.OldValue, e.NewValue));

        }

        public QuestionTemplateSelector():base()
        {
            Loaded += new RoutedEventHandler(OnLoaded);

        }

        private void OnLoaded(object sender, RoutedEventArgs e)
        {

            string questiontype = QuestionType;
            Debug.WriteLine(sender);


            if (questiontype == "Boolean")
            {
                Content = TemplateBoolean.LoadContent() as UIElement;
            }
            else if (questiontype == "Free Text")
            {
                Content = TemplateString.LoadContent() as UIElement;
            }
            else if (questiontype == "Single Multiple Choice")
            {
                Content = TemplateSingleMultipleChoice.LoadContent() as UIElement;
            }
            else if (questiontype == "Any Multiple Choice")
            {
                Content = TemplateAnyMultipleChoice.LoadContent() as UIElement;
            }
            else
            {
                Content = null;
            }
        }//onLoaded

    }//QuestionTemplateSelector

我觉得它与onloaded有关。我真的需要代码在Callback中,但因为它的静态我无法访问我需要的实例方法。我该怎么办?如果需要,我可以发布更多代码。

        public QuestionTemplateSelector():base()
        {
            Loaded += new RoutedEventHandler(OnLoaded);

        }

        private void OnLoaded(object sender, RoutedEventArgs e)
        {

            string questiontype = QuestionType;
            Debug.WriteLine(sender);


            if (questiontype == "Boolean")
            {
                Content = TemplateBoolean.LoadContent() as UIElement;
            }
            else if (questiontype == "Free Text")
            {
                Content = TemplateString.LoadContent() as UIElement;
            }
            else if (questiontype == "Single Multiple Choice")
            {
                Content = TemplateSingleMultipleChoice.LoadContent() as UIElement;
            }
            else if (questiontype == "Any Multiple Choice")
            {
                Content = TemplateAnyMultipleChoice.LoadContent() as UIElement;
            }
            else
            {
                Content = null;
            }
        }//onLoaded

我可以验证代码实际上是在回调中发生了变化,但CLR属性似乎永远不会更新。

1 个答案:

答案 0 :(得分:0)

这就是我的工作方式。但我不确定这是绝对正确的。

public class QuestionTemplateSelector : UserControl
    {
        public DataTemplate TemplateString { get; set; }
        public DataTemplate TemplateBoolean { get; set; }
        public DataTemplate TemplateSingleMultipleChoice { get; set; }
        public DataTemplate TemplateAnyMultipleChoice { get; set; }


        /// <summary>
        /// The <see cref="QuestionType" /> dependency property's name.
        /// </summary>
        public const string QuestionTypePropertyName = "QuestionType";

        /// <summary>
        /// Gets or sets the value of the <see cref="QuestionType" />
        /// property. This is a dependency property.
        /// </summary>
        public Int64 QuestionType
        {
            get
            {
                return (Int64)GetValue(QuestionTypeProperty);
            }
            set
            {
                SetValue(QuestionTypeProperty, value);
            }
        }

        /// <summary>
        /// Identifies the <see cref="QuestionType" /> dependency property.
        /// </summary>
        public static readonly DependencyProperty QuestionTypeProperty;

        private static void QuestionTypeChangedCallBack(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {

            ((QuestionTemplateSelector)obj).QuestionType = (Int64)e.NewValue;
            OnLoaded(obj, (Int64)e.NewValue);

        }



        static QuestionTemplateSelector()
        {
            QuestionTypeProperty = DependencyProperty.Register(
           "QuestionType",
           typeof(Int64),
           typeof(QuestionTemplateSelector), new PropertyMetadata(QuestionTypeChangedCallBack));

            //Loaded += new RoutedEventHandler(OnLoaded);

        }

        private static void OnLoaded(DependencyObject obj, Int64 e)
        {

            Int64 questiontype = e;
            if (questiontype == 1)
            {
                Debug.WriteLine("Boolean");
                ((QuestionTemplateSelector)obj).Content = ((QuestionTemplateSelector)obj).TemplateBoolean.LoadContent() as UIElement;
            }
            else if (questiontype == 2)
            {
                Debug.WriteLine("FreeText");
                ((QuestionTemplateSelector)obj).Content = ((QuestionTemplateSelector)obj).TemplateString.LoadContent() as UIElement;
            }
            else if (questiontype == 3)
            {
                Debug.WriteLine("Single Multiple Choice");
                ((QuestionTemplateSelector)obj).Content = ((QuestionTemplateSelector)obj).TemplateSingleMultipleChoice.LoadContent() as UIElement;
            }
            else if (questiontype == 4)
            {
                Debug.WriteLine("Any Multiple Choice");
                ((QuestionTemplateSelector)obj).Content = ((QuestionTemplateSelector)obj).TemplateAnyMultipleChoice.LoadContent() as UIElement;
            }
            else
            {
                Debug.WriteLine("NONE");
                ((QuestionTemplateSelector)obj).Content = null;
            }
        }//onLoaded



    }//QuestionTemplateSelector