c# - WPF: ListBoxItem with scroll bar -
is possible have listbox
(which contains scrollviewer
default) , listboxitem
scrollviewer
? want achieve next view:
and listbox should support virtualization. (i know how enable it, wondering work if use 2 scroll viewers?)
updated: need use listbox.itemtemplate
, since i'm binding itemssource
.
thanks tips.
easy. (i've given 3 ways this, 1 must right!)
through xaml:
<listbox x:name="listboxcontrol" horizontalalignment="left" height="320" verticalalignment="top" width="520"> <listboxitem width="520"> <scrollviewer horizontalscrollbarvisibility="visible" verticalscrollbarvisibility="disabled"> <label content="my name kidcode. reeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeaaaaaaaaaaaaaaaaaaaaaaaaly long comment."/> </scrollviewer> </listboxitem> </listbox>
from c#:
namespace stackexchange { /// <summary> /// interaction logic mainwindow.xaml /// </summary> public partial class mainwindow : window { public mainwindow() { initializecomponent(); var lbv = new listboxitemsv() { height = 40, width= 520, background = brushes.blue }; listboxcontrol.items.add(lbv); } public class listboxitemsv : listboxitem { scrollviewer sv = new scrollviewer() { horizontalscrollbarvisibility = scrollbarvisibility.visible, verticalscrollbarvisibility = scrollbarvisibility.hidden }; label lbl = new label() { content = "a really, really, really, really, really, really, really, really, really, really, really, really, really, really, really, long name." }; public listboxitemsv() { sv.content = lbl; this.content = sv; } } } }
this results in following: (i've added short comment can see difference, can make scroll bar go way across, example.)
if using item template: (i've never done this, don't shoot me if wrong! :) )
xaml:
<listbox x:name="listboxtwo"> <listbox.itemtemplate> <datatemplate> <scrollviewer horizontalscrollbarvisibility="visible" verticalscrollbarvisibility="disabled" width="520"> <label content="{binding}"/> </scrollviewer> </datatemplate> </listbox.itemtemplate> </listbox>
code behind:
list<string> mylist = new list<string>(); mylist.add("short name"); mylist.add("another short name"); mylist.add("a massively, hugely, giganticly, monstorously large name. (its bigger than think...............) "); listboxtwo.itemssource = mylist;
output:
Comments
Post a Comment