【问题标题】:Problem with Dependency Property依赖属性的问题
【发布时间】:2011-07-09 01:51:08
【问题描述】:

我正在使用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

我感觉它与加载有关。我真正需要代码的地方是回调,但由于它是静态的,我无法访问我需要的实例方法。我应该如何进行?如果您需要,我可以发布更多代码。

        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 属性似乎永远不会更新。

【问题讨论】:

  • 您还没有发布实际财产的声明。那看起来像什么?
  • @Gabe...抱歉,这是我的过度使用。我现在有完整的控件代码...
  • 所以让我直说吧! Callback 被调用,但在 Loaded eventHandler 中 QuestionType (clr) 属性返回 null?
  • 是的..这就是它的行为方式
  • 当我先调试加载的点击然后回调。

标签: silverlight-4.0 dependency-properties


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-31
    • 2011-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-01
    • 2019-04-17
    • 1970-01-01
    相关资源
    最近更新 更多