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 Javier,
Thank you for your post.
I have been looking into the description you have provided, but it seems like I am missing something from your scenario, so if this is still an issue for you, could you please provide me with more details about your views and models and the functionality that you are using? It will be great if you could share a simple sample project that illustrates your scenario. This way I would be able to further investigate this for you and provide you with more detailed information on this matter.
Also I can suggest you to take a look at the following links, where similar scenarios were discussed:
http://es.infragistics.com/community/blogs/blagunas/archive/2012/10/24/xamdatagrid-dynamically-create-and-data-bind-columns-with-an-editor-of-your-choice.aspx
http://stackoverflow.com/questions/3425972/mvvm-how-to-create-columns-at-runtime-for-a-xamdatagrid
Looking forward to hearing from you.
Thank you for your response. I can't provide a sample app because of security reasons but below is a more descriptive explanation.
I have a common view that's shared by different viewmodels, each of which represents different use cases like creating or amending orders. My view has a xamdatagrid and the fields are autogenerated based on the models of the viewmodels. One of the fields in one of my models uses a template field defined in the field layout of my xamdatagrid via xaml. I use a template field because I am using a custom control my team created as an editor for the field.
Unfortunately, since the template field is in the xamdatagrid of my common view, that means that every use case will have that field layout defined. This isn't a problem if the field is needed by the use case but if it isn't, the grid will end up having two field layouts - the one that's autogenerated based on the model and one that's defined in the xaml.
I want to create a template field without explicitly defining it in my xaml in order to avoid the above issue. Putting it in code behind might work but I'm not finding a good example of how.
Hello Amilcar,
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;
xdgrid.FieldLayouts[0].Fields.Add(tempField);
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.
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?
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"]);
}