Hey Guys,
I'm using a XamDataGrid (v15.2) with a TemplateField but I'd like to dynamically create this field instead of defining it in my XAML FieldLayout.
My grid is being used by a variety of ViewModels / Models each with different fields for different use cases. I'm using the TemplateField to define a custom field editor but it's possible that this field won't be needed for a use case. As a result, I end up with two FieldLayouts - one that's dynamically created using the fields in my Model (AutoGenerateFields = true) and one with the TemplateField.
Thanks in advance for your help!
Hello Amilcar,
Thank you for your feedback.
I am glad your solution is working now and I believe that other community members may benefit from this as well.
Please let me know if you require any further assistance on the matter.
Thanks again for your help. I realized I was adding the template field in the OnFieldLayoutInitialized handler. When I moved the code to add the template field to the OnFieldLayoutInitializing handler, it worked fine.
Hi Amilcar,
When the FieldLayout is auto-generated all fields are created based on the data in the source. In the above code snippet a new field is just added to the collection which is why we end up with a duplicated fields when there is FieldName property in the data source.
A check if FieldName field already exist could be done before the template field is added to the FieldLayout. This will allow you to remove the auto-generated one from the collection and add the new one.
if (xdgrid.FieldLayouts[0].Fields["FieldName"] != null)
{
xdgrid.FieldLayouts[0].Fields.Remove(xdgrid.FieldLayouts[0].Fields["FieldName"]);
xdgrid.FieldLayouts[0].Fields.Add(tempField);
}
Thanks for your response. It looks like this can work but I end up having two fields with the same name in my grid - the template field and the auto generated one. When I add it from xaml, the template field seems to replace the auto generated one. Is there a way to replicate this behavior?
The TemplateField can be added in code when it is needed. Using XamlParser will help you parse DataTemplates which are created in xaml code and add them as DisplayTemplate and EditorTemplate for the field. Then you can add the TemplateField to the already generated FieldLayout. Here is a code snippet for DisplayTemplate:
TemplateField tempField = new TemplateField();
tempField.Name = "FieldName";
tempField.Label = "FieldLabel";
string displayxaml = "<DataTemplate><customtemplate/></DataTemplate > ";
MemoryStream sr = new MemoryStream(Encoding.ASCII.GetBytes(displayxaml));
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("igEditors", "http://infragistics.com/Editors");
DataTemplate displaytmpl = (DataTemplate)XamlReader.Load(sr, pc);
tempField.DisplayTemplate = displaytmpl;
Here is a forum thread about XamlReader which can be helpful http://stackoverflow.com/questions/2481224/how-to-setup-a-wpf-datatemplate-in-code-for-a-treeview .
If you have any other questions, feel free to ask.