WPF : How do I attach an ObservableCollection to a Grid controls dynamically ?
-
Tuesday, May 22, 2012 9:52 AM
Hi,
I want to attach an ObservableCollection to the grid dynamically. The mebers of Collection source will be {String id, int rowIndex, List<String> textforButton, List<bool> isAdded}.
I add cols to the grid dynamically. And the grid will have 1 TextBlock (for id) and in other all cols Button. On each button click got to perform event actions. On other DataGrid's add/remove of rows, I got to add/remove rows from this row.
My point is how do I bind each row's col contents with the ObservableCollection<T> details. At present, I add rows dynamically as follows :
private void AddRowToGrid(String id) { RowDefinition newRow = new RowDefinition(); seivesTorGrid.RowDefinitions.Add(newRow); int rowIndex = seivesTorGrid.RowDefinitions.Count - 1; TextBlock idTB = new TextBlock(); idTB.Text = id; idTB.TextAlignment = TextAlignment.Left; idTB.VerticalAlignment = System.Windows.VerticalAlignment.Center; Grid.SetColumn(idTB, 0); Grid.SetRow(idTB, rowIndex); seivesTorGrid.Children.Add(idTB); for (int i = 1; i < 3; i++) { Button tempBtn = new Button() { Height=25, Width=80, Margin = new Thickness(5), Content = "Add", Background = Brushes.Brown }; tempBtn.Click += OnIdButtonClick; Grid.SetColumn(tempBtn, i); Grid.SetRow(tempBtn, rowIndex); seivesTorGrid.Children.Add(tempBtn); } }I am looking out is TextBlock.text, tempBtn.Conten to take from T source. If isAdded is changed then the respective button contents got to change.
Any one can help me with this. I want this to setup properly and procees ahead.
Any help is highly appreciated.
Thanks
If you find any answer helpful, then click "Vote As Helpful" and if it also solves your question then also click "Mark As Answer".
All Replies
-
Wednesday, May 23, 2012 7:36 AMModerator
Hi truptivd,
It is seemed that you add control into Grid Dynamically, right? And you want to know thow to bind with these controls in Grid to the observableCollection. You can set the ObservableCollection as the datacontext of the Grid, then you can just set bind to controls in Grid easily.
If I misunderstand you or you have any additional questions, please feel free to let me know.
Have a nice day.
Annabella Luo[MSFT]
MSDN Community Support | Feedback to us
-
Wednesday, May 23, 2012 8:53 AM
Hi Annabella,
Yes, I add controls dynamically. AS you see I add buttons based on a count (that's not stable), so how do I design me object for ObservableCollection. I want to bind text for the buttons.
I think I need to add List<String>, but can't get how to assign binding Path as something like myList[i]. I tried it but it doesn't work. I also tried making List<Button> for other cols, but that also didn't work out. So am stuck with that and can't make out how to do. Can you help me design the object for binding and how do I set the binding in Grid for respective rows/cols.
I also need to delete a row. If first this object design and binding is clear, then can work on delete also.
I request you to please support me to design an object for binding as per my needs and how to set the binding. I am stuck on this from last 2-3 days and can't get a way out. Please help me at the earliest.
Thanks
If you find any answer helpful, then click "Vote As Helpful" and if it also solves your question then also click "Mark As Answer".

