WPF: Listbox, centering selected item

为君一笑 提交于 2019-12-02 01:27:28

问题


Is it possible to always keep selected item in the middle of a listbox? If the user selects an item, I want to scroll so that the newly selected item is in the middle.

I guess it want be possible for the 'edge cases' (the first and last few items), but that's ok.


回答1:


David Anson posted some articles on his blog that might help you here: Part 1 and Part 2. He gives an extension method that centers an item in an List Box. You might be able to build on that.




回答2:


It is possible with couple of lines of custom code. Here's a discussion of a sample implementation. You might need to tweak it a bit to account for even number of items, insted of only odd number, but the general idea is the same.



来源:https://stackoverflow.com/questions/592911/wpf-listbox-centering-selected-item

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