ItemsPanelTemplate of a ListView throws apparently wrongly an exception

后端 未结 1 2033
隐瞒了意图╮
隐瞒了意图╮ 2021-01-06 18:26

I created a user control which contains a ListView with a custom ItemsPanelTemplate.



        
相关标签:
1条回答
  • 2021-01-06 18:51

    You are receiving this exception probably because you are trying to add "Children" to TimeLinePanel (or you are overriding the visual tree of the panel and returning something other than 1 in "VisualChildrenCount"). Sadly, you cannot modify the "Children" property of a panel if it is created inside an ItemsControl because of an ItemsPanelTemplate. Outside of an ItemsControl, there is no problem.

    Alternatively, you can override the template of ListView to something like the following - it works in my case, and gets rid of the exception.

    <ListView.Template>
        <ControlTemplate>
            <cd:TimeLinePanel IsItemsHost="True" UnitsPerSecond="..."/>
        <ControlTemplate>
    </ListView.ItemsPanel>
    

    There is a CodeProject article (http://www.codeproject.com/KB/WPF/ConceptualChildren.aspx) which gives some details on this behavior, which may help you understand why it is the case.

    0 讨论(0)
提交回复
热议问题