没有 ItemsPresenter 的 ItemsControl (Silverlight)
我创建了一个继承了ItemsControl的控件,以及我制作的控件 其模板没有 ItemsPresenter。相反,我有一个网格。 我想制作一个这样的列表:
(Item 1)
(GridSplitter)
(Item 2)
(GridSplitter)
(Item 3)
因为我必须在项目之间插入自己的 GridSplitter,因此我无法使用 ItemsPresenter 并让它为我填充 UI。
我重写 OnItemsChanged() 方法,以便我可以编写添加/删除的逻辑 网格分割器。但是,我仍然需要使用 ItemContainerGenerator 类来 为我的项目生成 ItemTemplate。
但是当我的控件模板中没有 ItemsPresenter 时,ItemContainerGenerator 似乎不起作用。当我第一次使用它生成项目时,它返回 带有 DataTemplate 的 ContentPresenter。然后我将它插入到我拥有的网格中。 但是,屏幕上没有显示任何项目。如果我添加另一个项目,它会抛出 当我插入网格时出现错误说该项目已经在可视化树中。 这就是我认为 ItemContainerGenerator 没有 ItemsPresenter 就无法工作的原因。
所以我尝试用 ItemsPresenter 替换我的 Grid。这次,它利用了它的 下划线创建项目的逻辑,它显示了它应该显示的内容。然而,这不是我想要的。
我想使用 ItemContainerGenerator 的原因是我需要创建 来自我在 Xaml 中定义的 ItemTemplate 的 UI 元素。
有没有一种方法可以使用项目控件的 ItemTemplate 生成 UI?
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 have 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您已经经历了监听 OnItemsChanged 并手动将内容添加到网格的麻烦,您应该只创建一个 UserControl 而不是使用 ItemTemplate,然后将 UserControl 的 DataContext 设置为适用的对象插入到您的Grid 并且不用担心从 ItemsControl 继承。
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.