Logo
CAR REVIEW
Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
PREGNANCY
 
 
Windows Phone

Programming Windows Phone 7 : The Intricacies of Layout - Text Concatenation with StackPanel

3/2/2011 10:55:19 PM
A StackPanel with a horizontal orientation can concatenate text. This is demonstrated in the TextConcatenation project:
Example 1. Silverlight Project: TextConcatenation File: MainPage.xaml (excerpt)
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<StackPanel Orientation="Horizontal"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Background="{StaticResource PhoneAccentBrush}">
<TextBlock Text="Two " />
<TextBlock Text="plus " />
<TextBlock Text="two " />
<TextBlock Text="equals " />
<TextBlock Text="four!" />
</StackPanel>
</Grid>

Here it is:



It might seem rather silly to concatenate text in this way, but it’s actually a very useful technique. Sometimes a program has some fixed text defined in XAML, mixed with some variable text from code or a data binding. The StackPanel does a nice job of piecing it together without any extraneous spacing. (In some cases you can alternatively use a TextBlock with its Inlines property set to multiple Run objects.)

Suppose you wanted the background color of the concatenated text to extend a little further beyond the boundaries of the text. You can’t do it with a Margin property on the StackPanel because that’s space outside the element. StackPanel doesn’t have a Padding property (alas), so you’d need to set Margin properties or Padding properties on all the individual TextBlock elements, and that doesn’t sound like fun.

An easier solution is to put the StackPanel in a Border element, and move all the alignment and Background settings to that Border:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Border Background="{StaticResource PhoneAccentBrush}"
Padding="12"
CornerRadius="24"
HorizontalAlignment="Center"
VerticalAlignment="Center">
<StackPanel Orientation="Horizontal">
<TextBlock Text="Two " />
<TextBlock Text="plus " />
<TextBlock Text="two " />
<TextBlock Text="equals " />
<TextBlock Text="four!" />
</StackPanel>
</Border>
</Grid>

Now you get a nice comfortable background with rounded corners:



Other -----------------
- Programming Windows Phone 7 : The Intricacies of Layout - The StackPanel Stack
- Programming Windows Phone 7 : The Intricacies of Layout - The Single-Cell Grid
 
 
Most view of day
- Nginx HTTP Server : Basic Nginx Configuration - Base module directives
- Designing an Update Management Strategy : Configuring an Update Testing Infrastructure, Verifying Update Deployment
- Windows Server 2003 on HP ProLiant Servers : Logical Structure Design (part 3) - Naming Standards
- Local Continuous Replications in Exchange Server 2007
- Sharepoint 2013 : New Installation and Configuration - SharePoint 2013 Prerequisites
- Sharepoint 2013 : Backup and Restore (part 3) - Unattached Content Database Data Recovery
- SQL Server 2008 R2 : Performance Monitoring Tools (part 12) - Viewing Data Collector Set Results in Performance Monitor
- SQL Server 2008 R2 : Executing Stored Procedures
- Protecting Windows from Viruses and Spyware : Antimalware Strategy: Defense in Depth (part 1) - Windows Action Center
- Microsoft Dynamic GP 2010 : Protecting Dynamics GP with key security settings
Top 10
- Microsoft Lync Server 2013 : Director Troubleshooting (part 3) - Synthetic Transactions,Telnet
- Microsoft Lync Server 2013 : Director Troubleshooting (part 2) - DNS Records, Logs
- Microsoft Lync Server 2013 : Director Troubleshooting (part 1) - Redirects, Certificates
- Microsoft Lync Server 2013 : Administration of the Director Role (part 4) - Services Management, Client Version Filter
- Microsoft Lync Server 2013 : Administration of the Director Role (part 3) - Topology Status
- Microsoft Lync Server 2013 : Administration of the Director Role (part 2) - Ports,Firewall Rules
- Microsoft Lync Server 2013 : Administration of the Director Role (part 1) - Services
- Microsoft Lync Server 2013 : Configuring the Director (part 2) - Web Services Ports,Reverse Proxy
- Microsoft Lync Server 2013 : Configuring the Director (part 1) - SRV Records, Web Services FQDN Overrides
- Sharepoint 2013 : SharePoint Designer 2013 (part 2) - Locking Down SharePoint Designer
 
 
Windows XP
Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
2015 Camaro