Silverlight 4.0 依赖属性的问题

Silverlight 4.0 依赖属性的问题,silverlight-4.0,dependency-properties,Silverlight 4.0,Dependency Properties,我使用一个代码作为参考,但我试图做的是不工作。支持属性永远不会改变…我不知道我所期待的是否是错误的 public class QuestionTemplateSelector : UserControl { public DataTemplate TemplateString { get; set; } public DataTemplate TemplateBoolean { get; set; } public DataTemplate

我使用一个代码作为参考,但我试图做的是不工作。支持属性永远不会改变…我不知道我所期待的是否是错误的

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

我可以验证回调中的代码是否确实在更改,但CLR属性似乎永远不会更新。

这就是我如何使其工作的。但我不确定这是否绝对正确

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
公共类问题TemplateSelector:UserControl
{
公共数据模板模板字符串{get;set;}
公共DataTemplate TemplateBoolean{get;set;}
公共数据模板模板SingleMultipleChice{get;set;}
公共数据模板TemplateAnyMultipleChice{get;set;}
/// 
///依赖项属性的名称。
/// 
公共常量字符串QuestionTypePropertyName=“QuestionType”;
/// 
///获取或设置
///属性。这是一个依赖属性。
/// 
公共Int64问题类型
{
得到
{
返回(Int64)GetValue(QuestionTypeProperty);
}
设置
{
SetValue(QuestionTypeProperty,value);
}
}
/// 
///标识依赖项属性。
/// 
公共静态只读DependencyProperty QuestionTypeProperty;
私有静态无效问题类型ChangedCallback(DependencyObject对象,DependencyPropertyChangedEventArgs e)
{
((QuestionTemplateSelector)obj).QuestionType=(Int64)e.NewValue;
空载(obj,(Int64)e.NewValue);
}
静态QuestionTemplateSelector()
{
QuestionTypeProperty=DependencyProperty.Register(
“问题类型”,
类型(Int64),
typeof(QuestionTemplateSelector),new PropertyMetadata(QuestionTypeChangedCallBack));
//已加载+=新的路由EventHandler(已加载);
}
已加载专用静态无效(DependencyObject obj,Int64 e)
{
Int64问题类型=e;
如果(问题类型==1)
{
Debug.WriteLine(“布尔”);
((QuestionTemplateSelector)obj.Content=((QuestionTemplateSelector)obj.TemplateBoolean.LoadContent()作为UIElement;
}
else if(问题类型==2)
{
Debug.WriteLine(“FreeText”);
((QuestionTemplateSelector)obj.Content=((QuestionTemplateSelector)obj.TemplateString.LoadContent()作为UIElement;
}
else if(问题类型==3)
{
Debug.WriteLine(“单选多选”);
((QuestionTemplateSelector)obj.Content=((QuestionTemplateSelector)obj.TemplateSingleMultipleEchoice.LoadContent()作为UIElement;
}
else if(问题类型==4)
{
Debug.WriteLine(“任意多选”);
((QuestionTemplateSelector)obj.Content=((QuestionTemplateSelector)obj.TemplateAnyMultipleEchoice.LoadContent()作为UIElement;
}
其他的
{
Debug.WriteLine(“无”);
((QuestionTemplateSelector)obj).Content=null;
}
}//装载
}//问题模板选择器

您尚未发布实际财产的声明。那是什么样子?@Gabe…很抱歉,这是我的失误。我现在有控制的完整代码了…让我把它弄清楚!调用了回调,但在加载的eventHandler中,QuestionType(clr)属性返回null?是的。这就是我首先调试加载的命中,然后调试回调时的操作方式。
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