Click here to Skip to main content
15,904,652 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
hi all
I bound the stack panel in load event I have 50 items in that panel first 10 items are visible to me I need to put two buttons up and down buttons while clicking on those buttons I need to move those items

What I have tried:

I just bind some items to stack panel
for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
{
Button[] Button = new Button[ds.Tables[0].Rows.Count];
Button[i] = new Button();
string s2 = ds.Tables[0].Rows[i][0].ToString();
string status = ds.Tables[0].Rows[i][1].ToString();
Button[i].Content = s2;
Button[i].Tag = s2;
Button[i].Click += btn_Click;
stackPanel1.Children.Add(Button[i]);
}

private void btnup_Click(object sender, RoutedEventArgs e)
{

}

private void btndown_Click(object sender, RoutedEventArgs e)
{

}
what should i do in these click events please help me..
Posted
Updated 3-May-16 1:47am

1 solution

The easiest way to do this is to use a ScrollViewer around the StackPanel.

For example here is my XAML:
HTML
<window x:class="LayoutWithStackPanel.MainWindow" xmlns:x="#unknown">
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <grid>
        <grid.rowdefinitions>
            <rowdefinition height="Auto" />
            <rowdefinition height="*" />
        </grid.rowdefinitions>
        <stackpanel orientation="Horizontal">
            <button content="Create" width="75" click="Button_Click" />
            <button content="Up" width="75" click="ButtonUp_Click" />
            <button content="Down" width="75" click="ButtonDown_Click" />
        </stackpanel>
        <scrollviewer grid.row="1" name="SvViewer" verticalscrollbarvisibility="Hidden">
            <stackpanel name="StackPanel1" orientation="Vertical">
            </stackpanel>
        </scrollviewer>
    </grid>
</window>

This will create a page with 3 buttons arranged along the top and a StackPanel below that will contain the generated buttons. Note that I've hidden the scrollbar for the ScrollViewer.

I placed your code into the Button_Click event to create lots of controls in the StackPanel StackPanel1.

The events for the other two buttons consist simply of
C#
private void ButtonUp_Click(object sender, RoutedEventArgs e)
{
    SvViewer.LineUp();
}
private void ButtonDown_Click(object sender, RoutedEventArgs e)
{
    SvViewer.LineDown();
}

or
C#
private void ButtonUp_Click(object sender, RoutedEventArgs e)
{
    SvViewer.PageUp();
}
private void ButtonDown_Click(object sender, RoutedEventArgs e)
{
    SvViewer.PageDown();
}
depending on how far you want to scroll.
 
Share this answer
 

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