2011-03-04 79 views
0

我正在使用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屬性似乎永遠不會更新。

+0

您尚未發佈實際屬性聲明。這是什麼樣子? – Gabe 2011-03-04 19:42:32

+0

@加貝......對不起,這對我來說太過分了。我現在有控制在那裏的完整代碼... – ecathell 2011-03-04 21:00:32

+0

所以讓我把它弄直! Callback被調用,但在Loaded事件處理程序中,QuestionType(clr)屬性返回null? – TDaver 2011-03-04 21:33:46

回答

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