Click here to Skip to main content
15,893,668 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am creating Gridview with dynamic columns as below.

C#
gridViewCol.DisplayMemberBinding = new Binding(string.Format("[{0}]", indexToUse));


I was binding the ListView Itemssource to
C#
ObservableCollection<List<string>>


This is working fine.

Now
We have a new requirement now that on these dynamic gridviewcolumn cells, I need to displays a ToggleButton/Popup depending on the data. The popup should displays a listbox.
For this I need to use GridViewColumn CellTemplate. But with datatemplate for CellTemplate in Xaml, how do I pass the index information(indexToUse above)?
ListView Itemssource now binds to
C#
ObservableCollection<List<ItemRow>> instead of ObservableCollection<List<string>>


ItemRow

C#
public class ItemRow : NotificationObject
    {
        private string cellItem = string.Empty;

        public string CellItem
        {
            get { return cellItem; }
            set
            {
                cellItem = value;
                RaisePropertyChanged(() => CellItem);
            }
        }

        private bool isMultiValued = false;
        public bool IsMultiValued
        {
            get { return isMultiValued; }
            set { isMultiValued = value; }
        }

        private List<string> multiValuedCellItem = new List<string>();
        public List<string> MultiValuedCellItem
        {
            get { return multiValuedCellItem; }
            set { multiValuedCellItem = value; }
        }
    }


I tried other alternatives to dynamically build the datatemplate like XAMLReader.Load and FrameworkElementFactory.
XAMLReader works but does not allow event handlers which is a problem.
FrameworkElementFactory does not work as required. Same is given per http://www.ikriv.com/dev/wpf/DataTemplateCreation/[^]

Using Relative source binding to get the column index also does not work. I cant use ElementName as columns are dynamic.
I tried using the similar solution as given here, but did not work http://www.thomaslevesque.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherited/[^]

Could anyone please suggest any other alternative solutions or suggestions on how to use Celltemplate with dynamic gridview columns.
Posted
Updated 20-Jul-15 21:33pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900