Problems with XamlReader generating DataTemplate
Asked Answered
P

2

6

I'm trying to implement the code below in my WPF project in order to generate DataTemplates on the fly for a DataGrid with dynamic columns. I found the code on StackOverflow here

public DataTemplate Create(Type type)
{
  return (DataTemplate)XamlReader.Load(
          @"<DataTemplate
            xmlns=""http://schemas.microsoft.com/client/2007"">
            <" + type.Name + @" Text=""{Binding " + ShowColumn + @"}""/>
            </DataTemplate>"
   );
}

However, on the XamlReader.Load code, I get the error "cannot convert from 'string' to 'System.Xaml.XamlReader'.

I tried to get around this by changing the code to:

return (DataTemplate)XamlReader.Load(XmlReader.Create(

but I get errors about passing invalid characters in the string.

Also, I am unsure how to pass a TextBlock to this code. I imagined I would just create a TextBlock and pass it as the Type argument, but I get the error "cannot convert from 'System.Windows.Controls.TextBlock' to 'System.Type'

Any help appreciated.

Phox answered 24/8, 2011 at 5:42 Comment(1)
How many distinct controls in that default namespace have a "Text" property?Onestep
A
10
public DataTemplate Create(Type type)
{
    StringReader stringReader = new StringReader(
    @"<DataTemplate 
        xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""> 
            <" + type.Name + @" Text=""{Binding " + ShowColumn + @"}""/> 
        </DataTemplate>");
    XmlReader xmlReader = XmlReader.Create(stringReader);
    return XamlReader.Load(xmlReader) as DataTemplate;
}

Call it like this

TextBlock textBlock = new TextBlock();
Create(textBlock.GetType());
Aboveground answered 24/8, 2011 at 6:31 Comment(8)
+1 for GetType but I don't think the XmlReader, StringReader is really necessary.Onestep
@AnthonyWJones: You are right, just using XmlReader.Create in XamlReader.Load will do.Aboveground
I'm not convinced the XmlReader is needed either. I do this sort of thing a lot and I just use a String. If there was a genuine error regarding invalid characters the use of XmlReader in that manner is not going to fix it.Onestep
@AnthonyWJones: I'm not sure how it works in Silverlight but in WPF, XamlReader.Load doesn't accept a string as parameter: msdn.microsoft.com/en-us/library/…. Do you have an alternate way?Aboveground
Good point, In Silverlight Load only takes a string. This is always a problem when questioners include both tags, we need to read the "fine print" to discern what they are actually working in. In this case Caustix is asking a WPF question. D'oh!Onestep
I may be ignorent, but what is the purpose of @ in the strings passed for the StringReaderTamah
Adarsha - it allows you to have a string on multiple lines without the needs for explicit concatenation (+)Supererogate
Problem with this is the template cannot use x:Bind.Irenics
E
0

I replicated your code with the workaround for XmlReader and it worked fine without any issues. Please try this:

 return (DataTemplate)XamlReader.Load(
                XmlReader.Create(
                    @"<DataTemplate  xmlns=""http://schemas.microsoft.com/client/2007""><" + type.Name + @" Text=""{Binding " + ShowColumn + @"}""/>
            </DataTemplate>"
             ));

This should work.

Efferent answered 24/8, 2011 at 6:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.