c# - How implement an infinity list in a combobox with int values -
i want implement dynamic combobox contain int values , dynamically load new elements before user reaching end of list.
so example load list initial value of 15 , list contains +- 5 element , when user reach +- 3. element end of list add 5 more element respectively end or beginning.
how can archive this?
to that, need handler scrollchanged
event of scrollviwer
control inside combobox
's controltemplate
.
first, edit combobox
template: visual studio designer selecte > edit template > edit copy,
in generated style search dropdownscrollviewer
, add scrollchanged
event handler
xaml
//.. <border x:name="dropdownborder" borderbrush="{dynamicresource {x:static systemcolors.windowframebrushkey}}" borderthickness="1" background="{dynamicresource {x:static systemcolors.windowbrushkey}}"> <scrollviewer x:name="dropdownscrollviewer" scrollchanged="onscrollchanged"> <grid x:name="grid" renderoptions.cleartypehint="enabled"> <canvas x:name="canvas" horizontalalignment="left" height="0" verticalalignment="top" width="0"> <rectangle x:name="opaquerect" fill="{binding background, elementname=dropdownborder}" height="{binding actualheight, elementname=dropdownborder}" width="{binding actualwidth, elementname=dropdownborder}"/> </canvas> <itemspresenter x:name="itemspresenter" keyboardnavigation.directionalnavigation="contained" snapstodevicepixels="{templatebinding snapstodevicepixels}"/> </grid> </scrollviewer> </border> //..
now, let combobox
bound observablecollection
:
<combobox x:name="cbbox" itemssource="{binding cboxitems}" verticalalignment="center" horizontalalignment="center" style="{dynamicresource comboboxstyle1}"/>
viewmodel
private observablecollection<string> _cboxitemscollection = new observablecollection<string>() { "0", "1", "2", "3", "4", "5", }; public observablecollection<string> cboxitems { { return _cboxitemscollection; } set { if (_cboxitemscollection == value) { return; } _cboxitemscollection = value; onpropertychanged(); } }
handler
the handler should that:
private void onscrollchanged(object sender, scrollchangedeventargs e) { var scrollviewer = (scrollviewer)sender; if (scrollviewer.verticaloffset == scrollviewer.scrollableheight) { (int = 0; < 5; i++) { cboxitems.add((cboxitems.count+i).tostring()); } } }
edit
i forgot mention scroll top, following same principle when verticaloffset
of scrollviewer
equal 0, insert negative items @ top of collection:
//.. if (scrollviewer.verticaloffset == 0) { (int = 0; < 5; i++) { cboxitems.insert(0,"negaive item"); } scrollviewer.scrolltoverticaloffset(10); } //..
the scrollviewer.scrolltoverticaloffset(10);
prevent infinite loop since default scrollviewer
scroll top.
Comments
Post a Comment