wpf
  • xaml
  • 2011-10-12 51 views 0 likes 
    0

    我正嘗試將自定義用戶控件加載到數據模板中,並且到目前爲止我擁有以下代碼。使用xaml解析器加載自定義用戶控件

    var xaml = @"<DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' 
                xmlns:rdb='clr-namespace:Admintool.UI.ResourceEditorWpf;assembly=program1.exe' > 
             <rdb:MaskedLabel 
              Content='{Binding " + e.PropertyName + "}'></rdb:MaskedLabel> </DataTemplate>"; 
    
         var stringReader = new StringReader(xaml); 
         var xmlReader = XmlReader.Create(stringReader); 
         var cellTemplate = (DataTemplate)XamlReader.Load(xmlReader); 
    

    其中,e.propertyname包含一個字符串。 運行此代碼我得到異常

    'rdb' is an undeclared namespace. Line 3, position 30. 
    

    誰能解釋如何正確引用的程序集在這種情況下?

    回答

    1
    <DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'> 
    xmlns:rdb='clr-namespace:Admintool.UI.ResourceEditorWpf;assembly=program1.exe' 
    

    rdb名稱空間在DataTemplate的結束標記之外聲明。它不應該是結束標記爲DataTemplate中像這裏面 -

    <DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' 
           xmlns:rdb='clr-namespace:Admintool.UI.ResourceEditorWpf;assembly=program1.exe'> 
    

    編輯: 你爲什麼不能有這樣的模板在XAML文件中聲明,並從那裏你可以使用XAML分析器加載它。嘗試看看這個鏈接 - http://blogs.msdn.com/b/ashish/archive/2007/08/14/dynamically-loading-xaml.aspx

    +0

    哎呀多數民衆贊成在一個錯字的問題,XAML仍然會拋出錯誤 – Marcom

    +0

    我已經更新了我的答案。請看一看 –

    相關問題