使用xaml解析器加载自定义用户控件

时间:2011-10-12 10:04:34

标签: wpf 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 个答案:

答案 0 :(得分: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

相关问题