A ItemsControl without a ItemsPresenter (Silverlight)
I created a control that inherited the ItemsControl, and the control that I made does not have a ItemsPresenter for its template. Instead, I have a Grid. I want to make a list like this:
(Item 1)
(GridSplitter)
(Item 2)
(GridSplitter)
(Item 3)
Because I ha开发者_StackOverflow中文版ve to insert my own GridSplitter between Items, thus I cannot use the ItemsPresenter and let it populate the UI for me.
I override the OnItemsChanged() method so I can write my logic of adding / removing the GridSplitter. However, I still need to use the ItemContainerGenerator class to generate the ItemTemplate for my Items.
But when there's no ItemsPresenter in my control template, the ItemContainerGenerator does not seem to work. When I use it to generate the item for the first time, it returns a ContentPresenter with a DataTemplate. Then I inserted it into the Grid that I have. However, there's no item showing up on the screen. If I add another item, it will throw an error when I insert into the Grid Saying that the item is already in the visual tree. That's the reason that I think the ItemContainerGenerator cannot work without the ItemsPresenter.
So I tried to replace my Grid with the ItemsPresenter. This time, it uses its underline logic to create items, and it displayed what it supposed to. However, it's not what I want.
The reason that I want to use the ItemContainerGenerator is that I need to create UI elements from the ItemTemplate that I defined in the Xaml.
Is there a way that I can generate the UI with the ItemTemplate of my control of the items?
If you're already going through the trouble of listening to the OnItemsChanged and manually adding your content to the grid, you should just create a UserControl instead of using an ItemTemplate, and then insert the UserControl with it's DataContext set to the applicable object into your Grid and not worry about inheiriting from the ItemsControl.
精彩评论