Проблема со свойством зависимости - PullRequest
0 голосов
/ 04 марта 2011

Я использую код веб-сайтов для справки, но то, что я пытаюсь сделать, не работает. Свойство поддержки никогда не меняется ... Я не знаю, неправильно ли то, что я ожидаю ...

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

У меня такое ощущение, что это связано с загруженным. Я действительно нуждаюсь в коде в 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 голосов
/ 05 марта 2011

Вот так у меня все получилось.Но я не уверен, что это абсолютно правильно.

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
...