【发布时间】: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