Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 代码隐藏中带有转换器的DataTemplate_C#_Wpf - Fatal编程技术网

C# 代码隐藏中带有转换器的DataTemplate

C# 代码隐藏中带有转换器的DataTemplate,c#,wpf,C#,Wpf,我正在尝试在代码隐藏中加载DataTemplate,但是如果我删除转换器,它可以正常工作。。。但我一把它放进去,它就爆炸了。现在,我确实将状态设置为名称空间,并将对转换器的引用放置在XAML中。例如: <Window.Resources> <local:StatCellConverter x:Key="myConverter" /> </Window.Resources> 我得到一个异常,它无法加载DataTemplate,这实际上是框架中的一个b

我正在尝试在代码隐藏中加载DataTemplate,但是如果我删除转换器,它可以正常工作。。。但我一把它放进去,它就爆炸了。现在,我确实将状态设置为名称空间,并将对转换器的引用放置在XAML中。例如:

<Window.Resources>
     <local:StatCellConverter x:Key="myConverter" />
</Window.Resources>

我得到一个异常,它无法加载DataTemplate,这实际上是框架中的一个bug。
通过XmlnsDictionary添加本地名称空间是行不通的。 必须将其添加到模板定义中,并定义程序集和命名空间:

正如@Nerd在培训中的上述评论所述,这应该是可行的:

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;
}
字符串statRowTemplate=”“;
专用数据模板GenerateStatRowDataTemplate()
{
ParserContext pc=新的ParserContext();
pc.XmlnsDictionary.Add(“,”http://schemas.microsoft.com/winfx/2006/xaml/presentation");
pc.xmlnsdirectionary.Add(“x”http://schemas.microsoft.com/winfx/2006/xaml");
pc.XmlnsDictionary.Add(“xcdg”http://schemas.xceed.com/wpf/xaml/datagrid");
字符串statRowTemplate=“”;
statRowTemplate+=“”;
statRowTemplate+=“”;
statRowTemplate+=“”;
statRowTemplate+=“”;
statRowTemplate+=“”;
statRowTemplate+=“”;
statRowTemplate+=“”;
statRowTemplate+=“”;
statRowTemplate+=“”;
StringReader StringReader=新的StringReader(statRowTemplate);
XmlReader=XmlReader.Create(stringReader);
MemoryStream ms=新的MemoryStream(Encoding.UTF8.GetBytes(statRowTemplate.ToString());
DataTemplate dt=(DataTemplate)XamlReader.Load(ms,pc);
dt.LoadContent();
返回dt;
}
完整版本

    var ms = new MemoryStream(Encoding.UTF8.GetBytes(@"<DataTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
                                                                     xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""                                                                             
                                                                     xmlns:c=""clr-namespace:MyApp.Converters;assembly=MyApp"">
            <DataTemplate.Resources>
                <c:MyConverter x:Key=""MyConverter""/>
            </DataTemplate.Resources>
            <TextBlock Text=""{Binding ., Converter={StaticResource MyConverter}}""/>
          </DataTemplate>"));
    var template = (DataTemplate)XamlReader.Load(ms);

    var cb = new ComboBox { };
    //Set the data template
    cb.ItemTemplate = template;
var ms=newmemoryStream(Encoding.UTF8.GetBytes(@)
"));
var template=(DataTemplate)XamlReader.Load(ms);
var cb=新组合框{};
//设置数据模板
cb.ItemTemplate=模板;

请发布您的转换器,并定义“它会爆炸”,我发现了。我必须在代码后面设置资源。。。字符串statRowTemplate=“”;statRowTemplate+=“”;@H.B.我没有足够的声誉来发布答案,否则我会这么做的.米开杰洛。。。既然你用我的答案回答了我的问题。。。我将给您Stackoverflow答案复选标记:)啊,通过
XmlnsDictionary
添加本地名称空间同样有效:重要的是像在xaml中那样指定程序集,从而产生以下行
pc.XmlnsDictionary.Add(“local”,“clr名称空间:MyTest;assembly=MyTest”);
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;
}
    var ms = new MemoryStream(Encoding.UTF8.GetBytes(@"<DataTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
                                                                     xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""                                                                             
                                                                     xmlns:c=""clr-namespace:MyApp.Converters;assembly=MyApp"">
            <DataTemplate.Resources>
                <c:MyConverter x:Key=""MyConverter""/>
            </DataTemplate.Resources>
            <TextBlock Text=""{Binding ., Converter={StaticResource MyConverter}}""/>
          </DataTemplate>"));
    var template = (DataTemplate)XamlReader.Load(ms);

    var cb = new ComboBox { };
    //Set the data template
    cb.ItemTemplate = template;