Silverlight set stackpanel position center of screen - silverlight

I'm using StackPanels in Silverlight 5 (VS2012), how can I center them in screen? is there any property (horizontalalignemt & verticalalignment do nothing), or should I center them using code?

You have to put stack panel in a container like Grid control then horizontal and vertical alignment properties should work.
<Grid x:Name="LayoutRoot" Background="White">
<StackPanel Width="50" HorizontalAlignment="Right"> </StackPanel>
</Grid>

Related

Why is this WPF border is showing around the column and not the StackPanel?

I'm trying to place a Border around a StackPanel like so:
<Border CornerRadius="5" BorderBrush="#009999" Background="Transparent"
BorderThickness="2" Grid.Column="2" Grid.Row="0">
<StackPanel HorizontalAlignment="Center" Margin="0,30,0,0">
<!--Contents-->
</StackPanel>
</Border>
For some reason, the border is appearing around the column that the StackPanel is in, not the StackPanel itself. I can change the margin of the border, but that doesn't hold up if the screen is resized. I also tried getting rid of the StackPanel margin but that made no difference. The StackPanel is the only object in this column.
Border has default HorizontalAlignment equal to Stretch, so it takes full possible width.
Then StackPanel is centered inside the border because it has HorizontalAlignment="Center".
set HorizontalAlignment="Center" on Border. It will get width, equal to StackPanel width (plus Margin), and then will be centered
I think it is because you're setting the HorizontalAlignment to Center. Doing this will make the StackPanel size to whatever its content is.
If you move the Border to stackpanel your problems will be fixed :)
here it is:
<Grid>
<StackPanel Width="350" Height="200">
<Border BorderThickness="2" BorderBrush="Black" Width="350" Height="200">
<TextBlock>SomeText</TextBlock>
</Border>
</StackPanel>
</Grid>

How to show scrollbars for the canvas wpf

I implemented the canvas from the code project example Drag controls in canvas
Here user has the ability to move anywhere in the canvas, when control reaches top or bottom, i want to show the vertical scrollbar. How can I make this work?
<ScrollViewer VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Visible">
<Grid Name="grid">
<Canvas Name="canvastoExport" >
<Polygon Points="200,100,300,100,300,200,200,200" Fill="Green"></Polygon>
</Canvas>
</Grid>
</ScrollViewer>

How do you control the height of a scrollviewer inside a tabitem?

I am having trouble getting the height of a scrollviewer to adjust to whatever the height is of a tabitem.
XAML:
<TabItem header="Item">
<ScrollViewer Height={Binding ActualHeight, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=TabItem}}>
<Grid>
.
.
.
</Grid>
</ScrollViewer>
</TabItem>
When I do this the scrollviewer is only about 50 pixels high. The content inside displays fine. The viewing area height is limited.
I tried this.
<TabItem header="Item" x:Name="itemname">
<ScrollViewer Height="{Binding ElementName="itemname", Path=ViewportHeight}">
The scrollviewer is not limited but when the content inside the
scrollviewer grows it goes beyond the viewing area and the scrollbar
deos not work.
I have used the first (FindAncestor) example with a StackPanel and it works. Not sure why it won't with a TabItem.
Why not use the HorizontalAlignment / VerticalAlignment properties?
<ScrollViewer HorizontalAlignment="Stretch" VerticalAlignment="Stretech">

How to re-height border for windows resize in XAML WPF

I have a more StackPanels in my XAML. Every StackPanel has a border inside.
When I modify the Main Window the width follows the resizing. But the height follows only in one the bigger direction. If I make the Window smaller the height of the borders doesn't follows. So the effect is the Botton border line isn't visible. How can I do this ?
<Window x:Class="MyStackPanel"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MyCombobox" Height="356" Width="475">
<Grid>
<StackPanel x:Name="STP"
Margin="10">
<Border x:Name="STPB"
BorderBrush="#FFE80707"
BorderThickness="5"
CornerRadius="10"
Height="{Binding ElementName=STP,Path=ActualHeight}"/>
</StackPanel>
</Grid>
</Window>
!border normaly Looks like
Use a Grid instead of a StackPanel then the border will stretch to the
Grids height and width on resizing
<Grid x:Name="STP"
Margin="10">
<Border x:Name="STPB"
BorderBrush="#FFE80707"
BorderThickness="5"
CornerRadius="10" />
</Grid>
Window after your changes, Looks like the same after reize (make smaller) without your changes.
http://i.stack.imgur.com/SplMn.jpg

Content scrolling with DockPanel

I'm facing problem with scrolling content in Dock panel .
My controls placed in 'DockPanel' as below
<DockPanel>
<ScrollViewer>
<StackPanel>
<!-- Here controls are like Radiobutton,Lable ,CheckBox,Textblock are added dynamically in grid.-->
</StackPanel>
</ScrollViewer>
I'm using only vertical scrollbar, not need horizontal scrollbar,
When I first time traverse through controls in 'DockPanel' by using tab ,Tab focus goes off the screen but panel is not scrolling down.
Please help me out I'm really stuck over here.
Thanks in advance.
Add IsTabStop="True" in Scrollviewer:
<DockPanel>
<ScrollViewer IsTabStop="True" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<StackPanel></StackPanel>
</ScrollViewer>
</DockPanel>

Resources