Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
Hi all

I ran odd problem with listbox and its scrollbar.

I have Wpf usercontrol which contains 2 rows. First row contains textboxes and combobox and in second row contains only listbox filled with dataclass.
The dataclass is presented in listbox trough itemtemplate which contains border, textblocks and image. I have no problem with presenting my class and itemtempalates works just fine, but when I have multiple objects in my dataclasses (let say eg. 100) so my listbox doest fit my window anymore so scrollbar is needed but It doesnt show!?

I have tried to set usercontrols rowheigth to 'auto', forced to show listbox verticalscrollbar, I have even wrapped listbox inside scrollwiever but every time scrollbar remains disabled or is not showed at all.. so I think something tells to my listbox that it has enough space.. Only Solution I have found is set listbox height to known size but that is not a option.

I made simple demo of my problem

Xaml:

<grid>
    <listbox grid.row="0" x:name="testlistbox" itemssource="{Binding}" scrollviewer.verticalscrollbarvisibility="Auto" xmlns:x="#unknown">
        <listbox.itemtemplate>
            <datatemplate>
                <grid>
                    <border borderthickness="2" borderbrush="Blue" cornerradius="10" margin="10" padding="10">
                        <stackpanel>
                                <textblock text="SampleText"></textblock>
                                <textblock text="SampleText"></textblock>
                                <textblock text="SampleText"></textblock>
                        </stackpanel>
                    </border>
                </grid>
            </datatemplate>
        </listbox.itemtemplate>
    </listbox>
</grid>


Code:

 Collection<int> SampleCollection = new Collection<int>();

            for (int i = 0; i < 30; i++)
            {
                SampleCollection.Add(i);
            }

            testlistbox.ItemsSource = SampleCollection;
</int></int>


Im sure that there is easy answer to this, but I havent found it.

UPDATE: I just found out that this code works just fine in wpf window BUT in usercontrol the problem remains.. This really confuses me.

UPDATE2: Well this is kinda shame to me, but the problem lied in layout than in listbox itself. I think this case is closed. Thanks all!

I hope you got idea :)

Cheers!
Posted
Updated 29-Mar-11 22:51pm
v3
Comments
Tarun.K.S 30-Mar-11 3:05am    
Please update your answer by showing the UserControl that you made as the demo that you provided works the way you wanted.
paleGegg0 30-Mar-11 4:27am    
Im afraid I cannot provide exact code but my example is basically same. I just found out that my problem shows in usercontrol but when copy paste to wpf window, it works...

1 solution

Set ScrollViewer.CanContentScroll="False" for the ListBox and see.

Update:
Your code in a sample application works perfectly for me. May be you are messing up your ListBox by placing it inside a complex layout.

Write up a sample application where you could repeat the scenario, and upload it in www.dropbox.com[^] or skydrive.live.com[^] and give the URL.
 
Share this answer
 
v3
Comments
paleGegg0 30-Mar-11 2:25am    
Tried, but does not fix the problem..
Venkatesh Mookkan 30-Mar-11 2:32am    
Answer updated
Tarun.K.S 30-Mar-11 2:38am    
From my side too its working perfectly.
paleGegg0 30-Mar-11 4:30am    
Did you make it in wpf window or usercontrol?
Venkatesh Mookkan 30-Mar-11 4:30am    
Its a Window

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900