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

标签 c# wpf

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

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

这是我生成 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;
}

我做错了什么?会不会我也必须在代码隐藏中定义我的转换器?

我的转换器

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();
        }
    }

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

最佳答案

这实际上是框架中的一个错误。 通过 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;
}

关于c# - 代码隐藏中带有转换器的 DataTemplate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7824493/

相关文章:

wpf - 防止 DataGrid 滚动吸附到行

c# - 将 CollectionViewSource 与 GroupDescriptions 一起使用时的 ListBox ScrollIntoView(即 IsGrouping == True)

c# - 未将对象引用设置为 LINQ to SQL 查询中的实例

c# - 安装的 Win 服务未显示在服务管理器中

c# - 如何获取ThreadContext类中的MethodInfo?

c# - 你如何按值对字典进行排序?

c# - 如何使用 SharpKml 创建 StyleMap 标签?

WPF捕获ObservableCollection的item属性变化

wpf - 为什么我的所有 WPF 应用程序自 Windows 10 (1809/1903) 以来都无法拖动到它们的窗口之外,例如调整窗口大小或进行拖放?

WPF - 阻止列表框项目选择