dynamic datatemplate with valueconverter

后端 未结 3 1683
逝去的感伤
逝去的感伤 2021-01-24 05:35

I want to show data in a wpftoolkit datagrid where the data is a collection of

public class Thing
{
    public string Foo { get; set; }
    public string Bar { g         


        
相关标签:
3条回答
  • 2021-01-24 06:08

    Would it help to declare the FooConverter once at a higher level (maybe as a resource of the DataGrid) instead of in each DataTemplate?

    0 讨论(0)
  • 2021-01-24 06:11

    For whatever reason, it works as expected if I do like this...

        string assName = Assembly.GetExecutingAssembly().GetName().Name;
        StringBuilder sb = new StringBuilder();
        sb.Append("<DataTemplate ");
        sb.Append("xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' ");
        sb.Append("xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' ");
        sb.Append("xmlns:src='clr-namespace:WpfToolkitDataGridTester;assembly=" + assName + "' >");
        sb.Append("<DataTemplate.Resources>");
        sb.Append("<src:FooConverter x:Key='fooConverter' />");
        sb.Append("</DataTemplate.Resources>");
        sb.Append("<TextBlock ");
        sb.Append("Foreground='{Binding Candidates[" + i + "].CandidateType,Converter={StaticResource fooConverter}}' ");
        sb.Append("Text='{Binding Candidates[" + i + @"].Name}' />");
        sb.Append("</DataTemplate>");
        var template = (DataTemplate)XamlReader.Parse(sb.ToString());
    
    0 讨论(0)
  • 2021-01-24 06:22

    When the XAML files are compiled to BAML it references the assembly not the in memory source. Since the BAML is compiled into the same assembly the actual type isn't available yet.

    I've found that a short term workaround is to comment out the style temporarily, build the project, then restore the style.

    The more permanent solution however is to move the converter to another assembly.

    0 讨论(0)
提交回复
热议问题