Positioning elements on a Silverlight Canvas

The Canvas.Top and Canvas.Left attributes can be set within the children control’s XAML to position within the Canvas relatively. <UserControl x:Class=’TestSilverlightApplication.MainPage’ xmlns=’http://schemas.microsoft.com/winfx/2006/xaml/presentation’ xmlns:x=’http://schemas.microsoft.com/winfx/2006/xaml’ xmlns:d=’http://schemas.microsoft.com/expression/blend/2008′ xmlns:mc=’http://schemas.openxmlformats.org/markup-compatibility/2006′ mc:Ignorable=’d’ d:DesignWidth=’640′ d:DesignHeight=’480′> <StackPanel Orientation="Vertical" Background="Blue"> <Canvas x:Name="MyCanvas" HorizontalAlignment="Left" VerticalAlignment="Top" Height="200" Width="400" Background="White" Margin="10"> <TextBlock Canvas.Top="10" Canvas.Left="15" FontSize="40" Foreground="Red" Text="Sample Canvas Text" /> <Rectangle Canvas.Top="40" Canvas.Left="50" Height="60" Width="100" Fill="Green" Canvas.ZIndex="-1" […]
Continue reading…

Enjoyed this post? Share it!

 
 

Set padding in silverlight code behind

Xaml: <UserControl x:Class=’TestSilverlightApplication.MainPage’ xmlns=’http://schemas.microsoft.com/winfx/2006/xaml/presentation’ xmlns:x=’http://schemas.microsoft.com/winfx/2006/xaml’ xmlns:d=’http://schemas.microsoft.com/expression/blend/2008′ xmlns:mc=’http://schemas.openxmlformats.org/markup-compatibility/2006′ mc:Ignorable=’d’ d:DesignWidth=’640′ d:DesignHeight=’480′>   <Canvas Name="canvas1"> </Canvas> </UserControl> Code behind: namespace TestSilverlightApplication { public partial class MainPage : UserControl { Button mybutton = null;   public MainPage() { InitializeComponent(); }   private void Window_Loaded(object sender, RoutedEventArgs e) { mybutton = new Button { Content = "Hi and Welcome", […]
Continue reading…

Enjoyed this post? Share it!

 
 

Change canvas background in Silverlight .Net

<UserControl x:Class=’TestSLApp.MainPage’ xmlns=’http://schemas.microsoft.com/winfx/2006/xaml/presentation’ xmlns:x=’http://schemas.microsoft.com/winfx/2006/xaml’ xmlns:d=’http://schemas.microsoft.com/expression/blend/2008′ xmlns:mc=’http://schemas.openxmlformats.org/markup-compatibility/2006′ mc:Ignorable=’d’ d:DesignWidth=’640′ d:DesignHeight=’480′>   <Canvas x:Name="LayoutRoot" Background="Red"> <Rectangle Canvas.Left="100" Canvas.Top="10" Width="40" Height="20" Fill="Yellow" /> </Canvas>   </UserControl>
Continue reading…

Enjoyed this post? Share it!

 
 

LineBreak and Run in Silverlight/WPF TextBlock

<Grid> <TextBlock> Hello World. <LineBreak />This is the second line. The breaking of the lines in the XAML are <Run Foreground="Red">not significant</Run>. </TextBlock> </Grid> A LineBreak indicates where line breaks are going to occur without regard to the TextWrapping property. A Run is used to wrap some piece of text that needs to be formatted […]
Continue reading…

Enjoyed this post? Share it!

 
 

Drawing a bezier curve in silverlight XAML

We can draw a curved line by populating the Data attribute to a PathGeometry element that specifies a Path and contains a BezierSegment (from 0,50 to 100,50 with control points of 50,0 and 50,100 as the curves). <Path Stroke="Black"> <Path.Data> <PathGeometry> <PathFigure StartPoint="0,50"> <BezierSegment Point1="50,0" Point2="50,100" Point3="100,50"/> </PathFigure> </PathGeometry> </Path.Data> </Path>
Continue reading…

Enjoyed this post? Share it!

 
 

Creating rows and columns in grid using XAML

Creating new columns and rows is done using the ColumnDefinitions and RowDefintions properties on a grid. This allows you to specify that individual elements are in a particular row or column using the attached property. <UserControl x:Class="silverlight.Sample" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition /> </Grid.ColumnDefinitions> <TextBlock Text="Hello" Grid.Column="1" /> </Grid> </UserControl>
Continue reading…

Enjoyed this post? Share it!

 
 

crossdomain.xml file for silverlight

In the following example, you are allowing access to all domains by setting the value * for the domain attribute. You can restrict the access to specific domains by defining them explicitly. The cross-domain policy files must be located in the root of the server. <?xml version="1. 0"?> <! DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd"> <cross-domain-policy> <allow-access-from […]
Continue reading…

Enjoyed this post? Share it!

 
 

Create a data object and bind it to the itemsource of the Treeview

Data Object Let us create a class called Person with Name, Age and Children as properties. public class Person { public Person() { Children = new ObservableCollection<Person>(); } public string Name { get; set; } public int Age { get; set; } public ObservableCollection<Person> Children { get; set; } } Treeview template <TreeView Name="trVwPerson" xmlns:obj="clr-namespace:TestWpf"> […]
Continue reading…

Enjoyed this post? Share it!

 
 

InstallState Enumeration in Silverlight

NotInstalled The application is running inside the browser and hasn’t been installed as an out-of-browser application. Installing The application is in the process of being installed as an out-of-browser application. InstallFailed The out-of-browser install process failed. Installed The application is installed as an out-of-browser application. This doesn’t necessarily mean it’s currently running outside of the […]
Continue reading…

Enjoyed this post? Share it!

 
 

Force silverlight application to work offline only

If you want to create an application that supports offline use only, you can use code like this in the Startup event handler: private void Application_Startup(object sender, StartupEventArgs e) { if (Application.Current.IsRunningOutOfBrowser) { // Show the full user interface. this. RootVisual = new MainPage(); } else { // Show a window with an installation message […]
Continue reading…

Enjoyed this post? Share it!

 
 

Explicitly installing an out-of-browser application in Silverlight

Below is the complete process for explicitly installing an out-of-browser application in Silverlight. private void cmdInstall_Click(object sender, RoutedEventArgs e) { // Make sure that the application is not already installed. if (Application.Current.InstallState ! = InstallState. Installed) { // Attempt to install it. bool installAccepted = Application. Current. Install();   if (! installAccepted) { lblMessage. Text […]
Continue reading…

Enjoyed this post? Share it!

 
 

Using Runs in Silverlight Textblock with different font styles

We can format individual bits of text but keep them together in a single paragraph in a TextBlock using a Run object inside the TextBlock element. Here’s an example that formats several words differently: <TextBlock FontFamily=" Georgia" FontSize="20" > This <Run FontStyle=" Italic" Foreground="YellowGreen">is</Run> a <Run FontFamily=" Comic Sans MS" Foreground="Red" FontSize="40">test.</Run> </TextBlock>
Continue reading…

Enjoyed this post? Share it!