背后带有代码的DataTemplate

Ner*_*ing 6 c# wpf

我正在尝试在代码隐藏区中加载DataTemplate,但是如果我删除Converter,它可以正常工作……但是,一旦将其放入其中,它就会崩溃。现在,我确实将状态设置为名称空间,并在XAML中放置了对转换器的引用。例如:

<Window.Resources>
     <local:StatCellConverter x:Key="myConverter" />
</Window.Resources>
Run Code Online (Sandbox Code Playgroud)

这是我生成DataTemplate的方法:

private DataTemplate GenerateStatRowDataTemplate()
{
    ParserContext pc = new ParserContext();
    pc.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
    pc.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml");
    pc.XmlnsDictionary.Add("xcdg", "http://schemas.xceed.com/wpf/xaml/datagrid");

    string statRowTemplate = "<DataTemplate><xcdg:StatRow>";
    statRowTemplate += "<xcdg:StatCell FieldName=\"Column4\" ResultPropertyName=\"AvgColumn4\">";
    statRowTemplate += "<xcdg:StatCell.ContentTemplate>";
    statRowTemplate += "<DataTemplate>";
    statRowTemplate += "<TextBlock Text=\"{Binding ., Converter={StaticResource ResourceKey=myConverter}}\" />";
    statRowTemplate += "</DataTemplate>";
    statRowTemplate += "</xcdg:StatCell.ContentTemplate>";
    statRowTemplate += "</xcdg:StatCell>";
    statRowTemplate += "</xcdg:StatRow>";
    statRowTemplate += "</DataTemplate>";

    StringReader stringReader = new StringReader(statRowTemplate);
    XmlReader xmlReader = XmlReader.Create(stringReader);
    MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(statRowTemplate.ToString()));
    DataTemplate dt = (DataTemplate)XamlReader.Load(ms,pc);
    dt.LoadContent();
    return dt;
}
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?难道我也必须在后面的代码中定义转换器吗?

我的转换器

public class StatCellConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            Debug.WriteLine(value);

            if (value.Equals("#DIV/0#"))
                return "0";
            return value;
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
Run Code Online (Sandbox Code Playgroud)

我得到一个异常,说它无法加载DataTemplate

小智 5

实际上,这是框架中的错误。通过XmlnsDictionary添加本地名称空间将不起作用。必须使用定义的程序集和名称空间将其添加到模板定义中:

就像@Nerd In Training的评论一样,它应该可以工作:

string statRowTemplate = "<DataTemplate >"; 

private DataTemplate GenerateStatRowDataTemplate()
{
    ParserContext pc = new ParserContext();
    pc.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
    pc.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml");
    pc.XmlnsDictionary.Add("xcdg", "http://schemas.xceed.com/wpf/xaml/datagrid");

    string statRowTemplate = "<DataTemplate xmlns:local=\"clr-namespace:MyTest;assembly=MyTest\" ><xcdg:StatRow>";
    statRowTemplate += "<xcdg:StatCell FieldName=\"Column4\" ResultPropertyName=\"AvgColumn4\">";
    statRowTemplate += "<xcdg:StatCell.ContentTemplate>";
    statRowTemplate += "<DataTemplate>";
    statRowTemplate += "<TextBlock Text=\"{Binding ., Converter={StaticResource ResourceKey=myConverter}}\" />";
    statRowTemplate += "</DataTemplate>";
    statRowTemplate += "</xcdg:StatCell.ContentTemplate>";
    statRowTemplate += "</xcdg:StatCell>";
    statRowTemplate += "</xcdg:StatRow>";
    statRowTemplate += "</DataTemplate>";

    StringReader stringReader = new StringReader(statRowTemplate);
    XmlReader xmlReader = XmlReader.Create(stringReader);
    MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(statRowTemplate.ToString()));
    DataTemplate dt = (DataTemplate)XamlReader.Load(ms,pc);
    dt.LoadContent();
    return dt;
}
Run Code Online (Sandbox Code Playgroud)