Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need help adding a ScrollViewer to a list on controls added at run time. With up to 50 rules being added in the code behind the top position on the canvas exceeds its design time size, and a scrolling view of the rules is necessary

I am adding a number of controls to a canvas at run time. The position on the canvas is determined at run time. When the number of controls forces the Canvas.SetTop(control, topPosition) to use a topPosition greater than the Canvas.Height property I want a scrollbar to appear.

I have added the ScrollViewer outside of the Canvas declaration. The ScrollViewer is visible, but appears disabled.

This is the XAML

XML
<GroupBox Name="QuickPickEntryCountGroupBox" Width="170" BorderThickness="0" Margin="190,751,0,0" >
            <ScrollViewer Name="QpRulesScrollViewer" VerticalScrollBarVisibility="Visible" CanContentScroll="True" >
             <Canvas Name="QPRulesCanvas" Height="50" Width="170" MaxHeight="130">
             </Canvas>
             </ScrollViewer>



The code behind is
foreach (var QPcount in dataAccess.pprqpdtab)
{
try
{
var pr = dataAccess.qta.spFetchPromoRuleEntryCount(promoGameId, promoDrawNumber, QPcount.Games);
ruleEntryCount = 0;
ruleGamesChecked = false;
if (pr != null)
{
ruleEntryCount = (int) pr;
ruleGamesChecked = true;
}
}
catch (Exception eRule)
{
exception handler code
}
var QPOption = new STDQPOptions(QPcount.Games, (int)ruleEntryCount, ruleGamesChecked);
placeElement(QPOption, leftpos, toppos);
toppos += 20;

}
XML
</pre>
Where placeElement(QPOption, leftpos, toppos)

is
<pre lang="cs">public void placeElement(UIElement control, int leftPosition, int topPosition)
{
    QPRulesCanvas.Children.Add(control);
    Canvas.SetLeft(control, leftPosition);
    Canvas.SetTop(control, topPosition);
}</pre>
Posted
Comments
Leung Yat Chun 23-Nov-13 0:23am    
Your ScrollViewer is not activated, because your Canvas doesn't fill the Grid.

Try:
<scrollviewer name="QpRulesScrollViewer" height="150" width="100" verticalscrollbarvisibility="Visible" cancontentscroll="True">
<Canvas Name="QPRulesCanvas" Height="300">

</Canvas>
</scrollviewer>

You have to expand your canvas manually, so a easier solution is to write a Panel that expand when needed, to replace the canvas.

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