Adding UI elements to scrolviewer dynamically, win phone - windows-phone-7

I know how to add children to panels. It is like this
stackpanel.children.add(uielement);
But I have a scrollviewer inside stackpanel.
I wanted to add textblocks and other elements inside the scrollviewer but there is no function to add children to scrollviewer.
I tried to add another stackpanel inside the scrollview and add elements to it... but if I add elements dynamically to the stackpanel defined inside scrollviewer the elements show right but there is no scroll. If I do the same that is:
<ScrollViewer>
<StackPanel x:Name="StckPanel">
<TextBlock Text="etc" />
</StackPanel>
</ScrollViewer>
Doing this enabled the scroll. But If I add elements to the stack panel using
StckPanel.Children.Add(Textblock Object);
It does add the elements but the scroll is now disabled.
Here is what I did:
The XAML code is
<ScrollViewer>
<StackPanel x:Name="Hello"></StackPanel>
</ScrollViewer>
In my c# file I did this:
TextBlock x= new TextBlock();
x.Text="Here it goes";
StckPanel.Children.Add(x);
But when I do this the scroll is disabled. I am not able to scroll.
So I need to know why is the scroll not working here. And how am I supposed to add child elements inside ScrollViewer.
As in XAML we can add UIElements inside the tags of ScrollViewer. Why cant we do the same using c#.

Well I found the problem. I was not specifying the height width and margins properly. As a result this messed up the scroll somehow.

Related

How to add more controls in Windows Phone 7 that is larger than screen size using ScrollViewer

In my Windows Phone 7 app, I just tried to add 10 controls in a page, but only 7 controls are visible in the page. I want to add remaining controls in that page which is above phones' default screen size.
When I googled this I found that the ScrollViewer control is used to scroll pages. So I added the ScrollViewer above my grid and set its vertical scrollbar visibility to true, but nothing changes as I'm not able to view the controls.
How do I write the XAML using a ScrollViewer to show all my controls?
ScrollViewer can have only one child under it, but it can be any kind of element or element container like a Grid for instance.
Just add all the elements to that container and then you will be able to scroll all items.
For example you can use a StackPanel since that will simply wrap all items under each one
<ScrollViewer>
<StackPanel>
<!-- All your controls -->
</StackPanel>
</ScrollViewer>
this is working
<ScrollViewer>
<StackPanel>
<!-- All your controls -->
</StackPanel>
</ScrollViewer>
bt in stack panel it goes as stack we cant move controls as we wish. to get rid of that we can use grid.
<ScrollViewer>
<Grid>
<!-- All your controls -->
</Grid>
</ScrollViewer>

How to make a scrollviewer gain focus when selected

I have a project where I have multiple scrollviews in one:
I can scroll up and down through a single item, and also through multiple items horizontally.
Normally, the webview is not hittestvisible.
Moving horizontally works fine, but when I scroll down, I give focus and hittestvisibility to the single item, but it won't scroll down. Only when I scroll down the second time, it will scroll, I think because when the ManipulationStartedevent was fired, it was caught by the scrollview, which had focus at the time, and only later the webview takes the focus, therefore, it has no started position. Is there a workaround for this problem?
<ScrollViewer x:Name="Scroller"
HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Disabled"
ManipulationMode="Control"
Grid.Row="1" Grid.RowSpan="2" >
<StackPanel Name="WebScrollView" Orientation="Horizontal" >
<UserControl Name="LeftContentControl" MinWidth="480" />
<UserControl Name="MiddleContentControl" MinWidth="480" />
<UserControl Name="RightContentControl" MinWidth="480" />
</StackPanel>
</ScrollViewer>
So, Left, Middle and Right are the three controls which will hold the webviews as their content. When scrolled to the left( for example ), the left content is placed over the middle content, and the middle content is set into view again, so it seems like an endless list of webviews, but really are three.
I use a mediator to achieve the animation of the webviews.
Thanks in advance.
GeekPeek

How to always show scroll bar in a listbox in wp7

How to always show scroll bar in a listbox. If I delete Notscrolling visualstate of Scrollviewer. It always shows scrollbar only after scroll down happend. I want to show scrollbar when page loaded it should be visible always.
Pls Help me
<ListBox
ItemsSource="{Binding}"
ScrollViewer.VerticalScrollBarVisibility="Visible">
</ListBox>
Hope it help
<ScrollViewer HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible">
<List Box ..>
</List Box>
</ScrollViewer>
The Thing is the scroll bar moves up/down (or left/right) to the maximum of the child's(here listbox) width/height value. So ensure that the parent of the scrollviewer has greater height/width than the child's of the scroll viewer.
For your question : This is by design WP will not display scroll bar until you scroll it. It comes visible once you scroll the content of the scroll viewer and then disapperars automatically. If it is displaying ever (as you want to be) it must be an overhead.

Windows Phone ListBox: how to scroll it?

I've got a ListBox (dynamically populated via code):
(i can't insert the code, bah)
I need, with a button, to move this listbox vertically. The listbox have a fixed height, and also the items.
Can someone help me?
Per MSDN, it looks like this is a viable answer for you?
ListBox.ScrollIntoView Method
Edit - Comment Summary of Answer: The question here was targetting scrolling itself, not to a particular object. As such, the necessary action was to attain a reference to the ListBox's ScrollViewer. From there, a call needed to be placed to the ScrollViewer's ScrollToVerticalOffset method. As the OP wanted to scroll down a certain amount from the original position, the ScrollViewer's VerticalOffset property was incorporated into the call.
This works. Set the ListBox to not scroll, then add a ScrollViewer around it. Now in your code behind you can set the ScrollViewer to whatever you want.
XAML:
<!--Disable the ListBox scroll and add a ScrollViewer so we have control over the scroll position.-->
<ScrollViewer
Name="scrlvwrListBoxMessages"
VerticalScrollBarVisibility="Auto" >
<ListBox x:Name="lstbxMessages"
ScrollViewer.VerticalScrollBarVisibility="Disabled" >
</ListBox>
</ScrollViewer>
Code:
private void ScrollToBottom()
{
//Scroll to the bottom.
Dispatcher.BeginInvoke(() =>
{
this.scrlvwrListBoxMessages.ScrollToVerticalOffset(double.MaxValue);
});
}

WP7 PanoramaItem with Icon/Button next to Header

Windows Phone 7's People hub has an "all" panorama item with "search" and a "new" buttons right next to the header/title.
I can't seem to accomplish this with PanoramaItem in Visual Studio using the standard Panorama control. I don't know enough Silverlight/WPF either to be able to position something manually and control transitions/movement correctly.
How can I set a button (or any object, for that matter) to go alongside the header of a wp7 PanoramaItem?
Thanks!
A PanoramaItem Header doesn't have to be a string. It could be another StackPanel that has a CheckBox in it if you wanted, something like this:
<controls:PanoramaItem>
<controls:PanoramaItem.Header>
<StackPanel Orientation="Horizontal">
<TextBlock>Item</TextBlock>
<CheckBox>CheckBox</CheckBox>
</StackPanel>
</controls:PanoramaItem.Header>
<Grid>
<TextBlock>Your Content</TextBlock>
</Grid>
</controls:PanoramaItem>
So you can basically put anything in there that you want. Images, buttons, checkboxes, etc.

Resources