2010-05-05 27 views
0

要求是創建可重複使用的多選組合框自定義控件。爲了做到這一點,我通過代碼動態創建DataTemplate並設置了組合框ItemTemplate。我可以動態加載數據模板並設置ItemTemplate,但在選擇組合框時會出現未處理的異常(代碼:7054)。使用Silverlight 3.0動態創建DataTemplate時獲取未處理的異常

下面是代碼

Class MultiSelCombBox: ComboBox 
{ 
    public override void OnApplyTemplate() 
    { 
     base.OnApplyTemplate(); 
     CreateTemplate(); 
    } 

    void CreateTemplate() 
    { 
     DataTemplate dt = null; 
     if (CreateItemTemplate) 
     { 
      if (string.IsNullOrEmpty(CheckBoxBind)) 
      { 
       dt = XamlReader.Load(@"<DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' x:Name=""DropDownTemplate""><Grid xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' x:Name=""CheckboxGrid""><TextBox xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' x:Name=""test"" xml:space=""preserve"" Text='{Binding " + TextContent + "}'/></Grid></DataTemplate>") as DataTemplate; 
       this.ItemTemplate = dt; 
      } 
     } 
    } 
    //Other code goes here }} 

我到底做錯了什麼?建議?

回答

0

錯誤代碼中的錯誤。不應該在每個元素上重新定義名稱空間。僅限頂層元素。