Question

Good afternoon everyone,

We have a XamDataGrid on our WPF application (VS 2010) and I need to programmatically add a control template for one field (fields are always added from code on our app).

So instead of creating the template on the XAML I am creating it from code (I followed several posts that explain similar issues) like this:

        Field f = new Field { Name = name, Label = label, Width = new FieldLength(20) }; 
        Style cellStyle = new System.Windows.Style(); 

        string templateStr = "<ControlTemplate TargetType=\"{x:Type igDP:CellValuePresenter}\">" + 
                                 "<Button Content=\"Flu\" />" + 
                            "</ControlTemplate>"; 

        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("igDP", "http://infragistics.com/DataPresenter"); 

        ControlTemplate ct = (ControlTemplate)XamlReader.Parse(templateStr, pc); 

        cellStyle.Setters.Add(new Setter() { Property = CellValuePresenter.TemplateProperty, Value = ((object)new CellValuePresenter() { Template = ct }) }); 
        f.Settings.CellValuePresenterStyle = cellStyle; 


        MainDataGrid.FieldLayouts[0].Fields.Add(f); 

Somehow this is not working and none of my fields/columns is displayed. So this is not working, any help will be appreciated.

Was it helpful?

Solution

Ok! I found it!

The problem was the way I was adding the setter to the ControlTemplate, so instead of:

cellStyle.Setters.Add(new Setter() { Property = CellValuePresenter.TemplateProperty, Value = ((object)new CellValuePresenter() { Template = ct }) });

It should be something like this:

cellStyle.Setters.Add(new Setter(CellValuePresenter.TemplateProperty, ct));

And then it works!

I know it is not the best way to do it but that's how I was asked to do it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top