WPF - ItemTemplate not acting as expected

后端 未结 5 2028
野的像风
野的像风 2021-01-14 02:25

I have a UserControl which I\'m using to display a list of UIElements. The control consists of a single ItemsControl with it\'s

5条回答
  •  -上瘾入骨i
    2021-01-14 02:52

    Nir is correct, ItemsControl will add item directly to its Panel if they are UIElements. I couldn't find any mention of this behavior in MSDN, but Dr. WPF mentions it in his article on item containers:

    If a UIElement is added to the Items collection of an explicit ItemsControl instance (as opposed to an instance of a derived class like ListBox), it will become a direct child of the items panel. If a non-UIElement is added, it will be wrapped within a ContentPresenter.

    Your solution is probably to use a ListBox instead, and set ItemContainerStyle to a new Style for ListBoxItem, and in that style, use a ControlTemplate with your Border in it.

提交回复
热议问题