Click here to Skip to main content
15,905,563 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
my scroll bar isn't showing up on the stackpanel this is my XML code

XML
<StackPanel Height="166" HorizontalAlignment="Left" Margin="325,1,0,0" Name="SP1" VerticalAlignment="Top" Width="620" Orientation="Horizontal" CanHorizontallyScroll="True" ForceCursor="False" SnapsToDevicePixels="True" OverridesDefaultStyle="True">
            <StackPanel.ScrollOwner>
                <ScrollViewer HorizontalContentAlignment="Center" CanContentScroll="True" PanningMode="HorizontalOnly" SnapsToDevicePixels="True" HorizontalScrollBarVisibility="Visible" DataContext="{Binding}">
                    <ScrollViewer.Background>
                        <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
                            <GradientStop Color="Black" Offset="0" />
                            <GradientStop Color="Red" Offset="1" />
                        </LinearGradientBrush>
                    </ScrollViewer.Background>
                </ScrollViewer>
            </StackPanel.ScrollOwner>
        </StackPanel>


and this is my code when i add the items

private void button1_Click(object sender, RoutedEventArgs e)
        {
            DateTime dt1 = Convert.ToDateTime(label1.Content + "/" + label2.Content + "/" + label5.Content);
            DateTime dt2 = Convert.ToDateTime(label3.Content + "/" + label4.Content + "/" + label6.Content);
            TimeSpan ts =  dt2-dt1 ;
            int days = ts.Days;
            label14.Content = ts.Days;
            for (int i = 0; i < ts.Days; i++)
            {
                TextBlock TXB = new TextBlock();
                TXB.Background = new LinearGradientBrush(Colors.Red, Colors.LightCyan, new Point(0, 0), new Point(1, 1));
                TXB.Text =  i.ToString();
                TXB.Width = 30;
                TXB.Name = "T" + i.ToString();
                this.SP1.Children.Add(TXB);
               
            }


and when it goes over the scroll bar doesn't show up, can anyone help me?
Posted

How about putting the Stackpanel inside a ScrollViewer.

XML
<ScrollViewer HorizontalContentAlignment="Center" CanContentScroll="True" PanningMode="HorizontalOnly" SnapsToDevicePixels="True" HorizontalScrollBarVisibility="Visible" DataContext="{Binding}">
<scrollviewer.background>
<LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
                            <GradientStop Color="Black" Offset="0" />
                            <GradientStop Color="Red" Offset="1" />
                        </LinearGradientBrush>
</scrollviewer.background>
<stackpanel name="SP1"></stackpanel>


Now the StackPanel should scroll.
 
Share this answer
 
v3
Comments
Tarun.K.S 12-Jun-11 4:51am    
Also close the ScrollViewer at the end.
[no name] 12-Jun-11 6:48am    
can you please tell me what i did wrong?
Tarun.K.S 12-Jun-11 7:59am    
Unfortunately, I am not sure what ScrollOwner property does. Here is the explanation that MSDN states: http://msdn.microsoft.com/en-us/library/system.windows.controls.stackpanel.scrollowner%28v=VS.90%29.aspx.
Tarun.K.S 12-Jun-11 8:00am    
Anyway did my solution work for you?
[no name] 12-Jun-11 8:25am    
i seem to get an error with that code i get ( Error 1 The attachable property 'background' was not found in type 'scrollviewer'. C:\Users\Kyle\documents\visual studio 2010\Projects\DaysUntill\DaysUntill\MainWindow.xaml 94 22 DaysUntill
)
Why are you making ScrollViewer as a child of the stack panel. Make the scroll viewer as a Parent of the stack panel and then add the child elements to the stack panel

try like that

<ScrollViewer CanContentScroll="True" SnapsToDevicePixels="True" HorizontalScrollBarVisibility="Visible"PanningMode="HorizontalOnly" DataContext="{Binding}" Width="300">
            <StackPanel Height="166" HorizontalAlignment="Left" Name="SP1" VerticalAlignment="Top" Width="620" Orientation="Horizontal" CanHorizontallyScroll="True" ForceCursor="False" SnapsToDevicePixels="True" OverridesDefaultStyle="True">
            </StackPanel>
        </ScrollViewer>


Whenever you need ScrollBar, then add that element under ScrollViewer. ScrollBar will automatically come.
 
Share this answer
 
Comments
Tarun.K.S 12-Jun-11 15:46pm    
Exactly what I was saying. 5+
nit_singh 12-Jun-11 15:59pm    
Thanks Tarun
[no name] 12-Jun-11 21:22pm    
I get this error


Error 1 'PanningMode' is an unexpected token causing the XML to be invalid. White space is expected. C:\Users\Kyle\documents\visual studio 2010\Projects\DaysUntill\DaysUntill\MainWindow.xaml 93 121 DaysUntill

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