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.

Community
  • 1
  • 1
Caustix
  • 569
  • 8
  • 26

2 Answers2

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());
H.B.
  • 166,899
  • 29
  • 327
  • 400
Fredrik Hedblad
  • 83,499
  • 23
  • 264
  • 266
  • 1
    +1 for GetType but I don't think the XmlReader, StringReader is really necessary. – AnthonyWJones Aug 24 '11 at 12:37
  • @AnthonyWJones: You are right, just using `XmlReader.Create` in `XamlReader.Load` will do. – Fredrik Hedblad Aug 24 '11 at 12:40
  • 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. – AnthonyWJones Aug 24 '11 at 12:49
  • @AnthonyWJones: I'm not sure how it works in Silverlight but in WPF, `XamlReader.Load` doesn't accept a string as parameter: http://msdn.microsoft.com/en-us/library/system.windows.markup.xamlreader.load.aspx. Do you have an alternate way? – Fredrik Hedblad Aug 24 '11 at 12:53
  • 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! – AnthonyWJones Aug 25 '11 at 08:57
  • I may be ignorent, but what is the purpose of @ in the strings passed for the StringReader – Adarsha Mar 25 '12 at 22:38
  • Adarsha - it allows you to have a string on multiple lines without the needs for explicit concatenation (+) – Chris Klepeis Apr 04 '14 at 14:48
  • Problem with this is the template cannot use x:Bind. – sjb-sjb Oct 06 '18 at 12:55
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.

Mamta D
  • 6,310
  • 3
  • 27
  • 41