Siliverlight: controls visibility changes alignment? - silverlight

The following code
<StackPanel Orientation="Horizontal">
<Image Source="test.jpg"/>
<Image Source="test2.jpg"/>
<TextBlock Text="TeStBlock"/>
</StackPanel>
If I am setting the visibility of the contents of the StackPanel in the codebehind and lets say I set the visibility of the Second image to collapsed. I notice that the TextBlock moves to where the Image used to be.
How can I keep the alignment and turn the visibility on or off?

Use Opacity="0" instead.

Related

How do I get my Stack Panel to stretch all the way across this header?

In the header of my Expander I am trying to place a few buttons. I want these buttons to be on the far right, however any element I add inside the header is reduced to it's minimum size regardless of the horizontal alignment rules.
<StackPanel Grid.ColumnSpan="4" Grid.RowSpan="4">
<Expander ExpandDirection="Down" IsExpanded="True" FontSize="14" FontWeight="Bold" Margin="5" BorderThickness="1" BorderBrush="#FF3E3D3D">
<Expander.Header>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch">
<TextBlock Text="Greeting and Opening" VerticalAlignment="Center" HorizontalAlignment="Center"/>
<Button x:Name="GreetingCheckAll_Button" Content="Check All" HorizontalAlignment="Center" Margin="5,0,0,1" VerticalAlignment="Center"/>
</StackPanel>
</Expander.Header>
I want to get that button to the far right instead of to the left, as you can also see the stack panel element is only as large as it needs to be. What do I need to do to allow for the stack panel to stretch across the entire width so I can align the button to the right?
Thanks guys.
Edit: Tried using the docking panel suggestion instead (after looking at the panels overview, that seems the right way to go). I still end up with the same constraints, the panel will not stretch across the entire width of it's parent element unless I manually set it's width in pixels.
What you want is not a StackPanel, you want something like the DockPanel or Grid
Where you set the first Content as TextBlock, then Dock a button to the right.
Or a Grid.

StackPanel Orientation in xaml

I got a weird problem on my xaml, in a stackPanel.
My stackPanel contains a textbox, and a button.
This should be on the same line (if possible, depending on the text width).
The problem is :
if the stackPanel have Orientation="Vertical", the button will go to the line bellow the text.
if the stackPanel have Orientation="Horizontal" , the line will not doing any break line, so all the line will go out of my grid.
<StackPanel Name="spRemplir"
Grid.Row="2"
Grid.ColumnSpan="6"
Width="560"
Margin="5,5,5,5"
Orientation="Horizontal"
VerticalAlignment="Center">
<TextBlock FontWeight="Bold"
Text={Binding Text}
TextWrapping="Wrap"/>
<Button Name="btRemplir"
Margin="5,0,0,0"
Width="150"
Content="Remplir"/>
</StackPanel>
How can I obtain a stackPanel, that will break lines if necessary, and have a text and a button on the same line?
Update with Wrapanel thanks to Eli Arbel :
<toolkit:WrapPanel Name="spRemplir"
Grid.Row="2"
Grid.ColumnSpan="6"
Margin="5,5,5,5"
Width="560"
Orientation="Horizontal">
<TextBlock FontWeight="Bold"
Text={Binding Text}
TextWrapping="Wrap"/>
<Button Name="btRemplir"
Content="Remplir"/>
</toolkit:WrapPanel>
But, the button still on the next line, while there is enough space after the text on the same line.
Itried removing the Width on the panel, but then, there is no more wrapping...
I don't understand. Even if there is stackPanel on the same Grid, they should not disturb the wrap panel right?
Thank you.
You can try a WrapPanel from the Silverlight Toolkit. Note this panel will not allow you to stretch the items to the container width.
You should also look into removing the fixed Width of the panel.
Try using a Grid instead of a StackPanel. The problem with StackPanel is that they do not report up the Visual Tree that they are out of room. This is not a bug, it's just the way they are and it's appropriate when you need it. But I avoid them except on the innermost elements. But don't have StackPanels in StackPanels as you will lose TextWrapping/Scrolling and just have elements fall of the right or bottom of the page.
Second, make sure that your outer container is Set so that the width is Constrained. For example, in your layout root, give it one Column, and set the Width for * which means = "The available space, but not more"
Once you have your outer container constrained, then your TextBlock will wrap properly.
Greg
Consider using a WrapPanel instead stack panel: http://wpftutorial.net/WrapPanel.html

Why doesn't my StackPanel's background color stretch to fill the full width?

I have a StackPanel with a background brush, and it contains a single button. It is contained within a Grid that is wider than the button.
Frustratingly, the background is only as wide as the button. I can see it around the button's margin. So I end up having this wide bar of default-gray next to my button which has a border of my background color around it.
<StackPanel Visibility="Visible" HorizontalAlignment="Right" Background="{StaticResource qtyBg}">
<Button x:Name="bttnQtyEditKeys" Content="EDIT KEYPAD" Visibility="Visible" Click="bttnQtyEditKeys_Click"/>
</StackPanel>
I was able to fix it by wrapping a Border around it, and setting the Border's background brush.
It's a hack, so it it annoys me. Is there an alternate way to make the StackPanel's background not be stupid?
The problem isn't your background, it's your StackPanel. Your background is fine, it's your StackPanel that isn't filling the available space.
You need to set HorizontalAlignment and VerticalAlignment to Stretch on your StackPanel.
This works fine for me:
<Grid>
<StackPanel Visibility="Visible" HorizontalAlignment="Stretch" Background="blue">
<Button x:Name="bttnQtyEditKeys" HorizontalAlignment="Right" Content="EDIT KEYPAD" Visibility="Visible" Click="bttnQtyEditKeys_Click"/>
</StackPanel>
</Grid>

How do I freely move a textblock around in a Border in Windows Phone 7

I cannot seem to freely move a textblock around wherever I want inside a border control. It always snaps to the center. How can I stop this from happening?
This is my markup:
<Border BorderThickness="0" Height="258" Margin="30,206,20,188" Name="border2" Width="406" >
<Border.Background>
<ImageBrush ImageSource="/TestApp;component/Images/blurb.png"/>
</Border.Background>
<TextBlock Height="103" x:Name="tBlk_Steps" Text="{Binding Count}" Width="298" FontSize="56" Foreground="#FF00BCE4" ></TextBlock>
</Border>
Use the HorizontalAlignment and VerticalAlignment properties on the Textblock. You can also control it with the Margin on the TextBlock.
/Anders
Try VerticalAlignment="Top" and HorizontalAlignment="Left"
Alternatively try setting Margin properties on the TextBlock.
One great way to work this sort of thing out is to open up a project in ExpressionBlend - it's Visual designer drives me crazy, but it does also help with some things!

Silverlight: TextBox VerticalContentAlignment="Center"

I'm trying to vertically center the content of a TextBox with the VerticalContentAlignment property but it seems to have no effect at all. The text stays at the top. Can anyone tell me how to do this?
Here's my code:
<TextBox Grid.Column="1"
Grid.Row="0"
Width="200"
Height="28"
VerticalAlignment="Center"
VerticalContentAlignment="Center" />
It is possible to make the TextBox center its text vertically. However, that does require you to reapply its ControlTemplate.
To do this:
Copy the Style and the ControlTemplate from the TextBox Styles and Templates page on MSDN to a suitable <UserControl.Resources> element. (This ControlTemplate is actually for a validation tooltip; the ControlTemplate we'll change is within the Style.)
Find the ScrollViewer element within the Style for the TextBox, and add a VerticalAlignment="Center" property to it.
Alternatively, you could add the property
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
to the ScrollViewer. This should allow you to set the vertical alignment of the contents of your TextBoxes using the VerticalContentAlignment property.
You can follow much the same approach if you wish to change the horizontal alignment of a TextBox's content as well.
The XAML code is correct, the following should be sufficient:
<TextBlock Text="Centered Text" VerticalAlignment="Center" />
Can you try that code outside your grid?
Check the attributes you defined on your Grid, this will probably cause the behaviour you have. Can you post the complete XAML code?

Resources