How to add children of an ItemsControl where the children should decide their own position in Wpf?

耗尽温柔 提交于 2019-12-24 05:57:46

问题


I am using a wpf slider to display the time line in a video player. I need to add an ItemControl of some sort on top of this so that I can add buttons on the time line on certain positions in the time line (the buttons will hold their own position relative to the parent ItemsControl).

What ItemsControl container should I use where I can add child elements that know their own position (based on their timecode)? I have looked at the different ItemsControls in Wpf, and it looks like all of them have a certain way to stack their children (Horizontal or vertical one after another)!

And to add some more complexity, How can the positioning of the children be relative to the width of the parent ItemsControl? This way scaling up and down the ItemsContol will reposition the children!


回答1:


The WPF control that allows explicit positioning is the Canvas. You can then set Canvas.Left and Canvas.Top on child controls as necessary.

You can make an ItemControl use a Canvas for layout like this:

<ItemsControl>
  <ItemsControl.ItemsPanel>
    <ItemsPanelTemplate>
      <Canvas IsItemsHost="True" />
    </ItemsPanelTemplate>
  </ItemsControl.ItemsPanel>
</ItemsControl>

Edit: missed the resizing...

If you use a ViewBox around the ItemsControl the content will automatically resize, but the items themselves will also get larger. If that's not suitable then I think you'll have to manually reposition the controls, since the Canvas doesn't allow that kind of positioning.



来源:https://stackoverflow.com/questions/2408056/how-to-add-children-of-an-itemscontrol-where-the-children-should-decide-their-ow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!