jwheeler

I have an ItemsControl containing what are essentially nested horizontal forms

---ItemsControl------------------------------------------------
Date _______________ Description ________________ [ADD][DEL] |
Date _______________ Description ________________ [ADD][DEL] |
Date _______________ Description ________________ [ADD][DEL] |
Date _______________ Description ________________ [ADD][DEL] |
---EndItemsControl---------------------------------------------

The ItemsControl is backed by an ObservableCollection and uses a DataTemplate to create and bind the 'subforms' to business objects in this collection.

I have an event handler hooked up to the [ADD] button, which simply appends an element to the end of the collection. The [DEL] button removes the last element.

I would like the [ADD] button to add a row underneath it and the [DEL] button to delete the current row. I have tried using ItemsControl.Items.CurrentPosition, but it always reports the first element in the list.

Does anyone have an idea of how to solve this Is the ItemsControl the way I should approach dynamic form construction in general

Regards,
John


Re: Windows Presentation Foundation (WPF) How to Get the Position of a Nested ItemsControl Control?

jwheeler

I was able to handle this by binding the source object itself to the tag property of each of the add and delete buttons. I call ItemsControl.Items.IndexOf on the tag to get the index. I am interested in hearing about alternative/better solutions and thoughts in general about this technique.




Re: Windows Presentation Foundation (WPF) How to Get the Position of a Nested ItemsControl Control?

jwheeler

The thing I don't like about this is it relies on the equals implementation which might not be suitable. Isn't there a better way using Bubbled or Tunneled events How might I go about it




Re: Windows Presentation Foundation (WPF) How to Get the Position of a Nested ItemsControl Control?

Josh Smith

Never forget about the visual tree! When the [DEL] button is clicked, in the Click event handler, you can use VisualTreeHelper to walk up the visual tree (starting with e.OriginalSource) until you hit the ContentPresenter which hosts the Button. At that point, pass the ContentPresenter into myItemsControl.ItemContainerGenerator.IndexFromContainer(theContentPresenter). That will give you the visible index of the "row" which was clicked.




Re: Windows Presentation Foundation (WPF) How to Get the Position of a Nested ItemsControl Control?

jwheeler

Thanks Josh, I had to walk up till I got to the ListViewItem otherwise -1




Re: Windows Presentation Foundation (WPF) How to Get the Position of a Nested ItemsControl Control?

Josh Smith

D'oh! Why did I write ContentPresenter ! Big Smile

I'm glad you got it working.






Re: Windows Presentation Foundation (WPF) How to Get the Position of a Nested ItemsControl Control?

jwheeler79

Hey Josh,

I used the information in this thread to write part of an article for Devx. I gave you due credit here: http://www.devx.com/dotnet/Article/35135/0/page/3

John