WP Beginners How to: Programmatically set common Control properties
published on: 8/16/2011 | Views: N/A | Tags: GetStarted
by WindowsPhoneGeek
While browsing through the WP Dev forums I noticed that lots of developers are asking similar questions about how to set different control properties via code. So in this post I am going to answer these questions with examples:
Question1: How to programmatically wrap the TextBlock Text?
Answer:
this.textBlockWrapped.TextWrapping = TextWrapping.Wrap;
Here is how the same code would look like in XAML:
<TextBlock x:Name="textBlockWrapped" TextWrapping="Wrap" Text="This is a very long text in a TextBlock that should be wrapped."/>
Question2: How to programmatically change SvrollViewer Visibility?
Answer:
this.scrollViewer.HorizontalScrollBarVisibility = ScrollBarVisibility.Visible; this.scrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
Here is how the same code would look like in XAML:
<ScrollViewer x:Name="scrollViewer" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Visible">
<!--some code here...-->
</ScrollViewer>
Question3: How to change a control's Font programmatically?
Answer:
this.textBlock.FontSize = 30;
this.textBlock.FontFamily = new FontFamily("Arial");
this.textBlock.FontWeight = FontWeights.Bold;
Here is how the same code would look like in XAML:
<TextBlock Text="TextBlock" FontWeight="Bold" FontSize="30" FontFamily="Arial" x:Name="textBlock"/>
Question4: How to programmatically change theme colors?
Answer:
(App.Current.Resources["PhoneAccentBrush"] as SolidColorBrush).Color = Color.FromArgb(12,12,54,145); (App.Current.Resources["PhoneForegroundBrush"] as SolidColorBrush).Color = Colors.Green; (App.Current.Resources["PhoneBackgroundBrush"] as SolidColorBrush).Color = Colors.Purple; Question5: How to programmatically add controls to a Grid/Canvas?
Answer:
Here is how the same code would look like in XAML:
Button btn = new Button(); //ContentPanel is the main Grid in MainPage.xaml this.ContentPanel.Children.Add(btn);
Question6: How to programmatically change background Color?
Answer:
this.button.Background = new SolidColorBrush(Colors.Green); this.button.Background = new SolidColorBrush(Color.FromArgb(10,15,100,124));
Here is how the same code would look like in XAML:
<Button x:Name="button" Content="Change Color" Background="Green"/>
Question7: How to programmatically change background Image?
Answer:
ImageBrush imageBrush = new ImageBrush();
Uri uri = new Uri("logo.png", UriKind.RelativeOrAbsolute);
imageBrush.ImageSource = new BitmapImage(uri);
this.button.Background = imageBrush;
Here is how the same code would look like in XAML:
<Button x:Name="button" Content="Image" Width="100" Height="100">
<Button.Background>
<ImageBrush ImageSource="logo.png"/>
</Button.Background>
</Button>
Question8: How to programmatically change the margin of a Control?
Answer:
this.btn.Margin = new Thickness(20, 2, 2, 2);
Here is how the same code would look like in XAML:
<Button Content="Button" Margin="20,2,2,2" x:Name="btn"/>
Question9: How to programmatically change BorderThickness?
Answer:
this.border.BorderThickness = new Thickness(5, 4, 3, 2); this.border.BorderThickness = new Thickness(5);
NOTE: The thickness components are in the following order: Left, Top, Right, Bottom.
Here is how the same code would look like in XAML:
<Border BorderBrush="Red" x:Name="border" Height="200" Width="200" BorderThickness="5,4,3,2">
<!--Some code here...-->
</Border>
Here is the full source code with all examples:
I hope that the post was helpful.
You can also follow us on Twitter @winphonegeek
Comments
Thank you for sharing this nfo.
posted by: P.Jamar on 8/16/2011 7:19:59 PM
Thank you for sharing this info. It is very helpful for beginners like me. Looking foreword for the next beginners tutorial.
Great
posted by: ViEL on 9/24/2011 5:59:28 PM
Great!! Thanks
Our Top Articles & Free books
- Our FREE e-book: "Windows Phone Toolkit In Depth" 2nd edition
- 400+ Windows Phone Development articles in our Article Index
- 21 WP7 Toolkit in Depth articles covering all controls
- 12 WP7 Coding4Fun Toolkit in Depth articles covering all controls
- Performance Tips when creating WP7 apps
- Creating a WP7 Custom Control in 7 Steps
- WP7 working with VisualStates: How to make a ToggleSwitch from CheckBox
- What makes a WP7 App successful
- Creating theme friendly UI in WP7 using OpacityMask
- Implementing Windows Phone 7 DataTemplateSelector and CustomDataTemplateSelector
- All about Splash Screens in WP7 – Creating animated Splash Screen
- Getting Started with Unit Testing in Silverlight for WP7
- WP7 WatermarkedTextBox custom control
Our Top Tips & Samples
- All about WP7 Isolated Storage series
- WP7 Dynamically Generating DataTemplate in code
- 5 tips for a successful WP7 Marketplace submission
- WP7: Navigating to a page in different assembly
- WP7 ContextMenu: answers to popular questions
- WP7 ListBox: answers to popular questions
- WP7 working with Images: Content vs Resource build action
- WP7 Element Binding samples
- WP7 working with XML: reading, filtering and databinding
- Drawing in WP7: #2 Drawing shapes with finger
- WP7 TextBox Light theme problems - the solution
- Changing the WP7 Panorama Background Image dynamically with Animation
