Recommended

  • Silverlight 4 Podcast Pack with Tim Heuer
  • Building Modular Silverlight Applications
  • Prism -  10 Things to Know
  • Securing Silverlight Application and WCF Service using ASP.Net Authentication Techniques
  • Model– View – ViewModel in Silverlight
Skip Navigation LinksHome / Tips

Tips

+
Page 
Items Resolution

  • 0 comments  /  posted by  Denislav Savkov  on  Sep 03, 2008 (more than a year ago)
    Tags: Silverlight , Grid , columns , rows

    The System.Windows.Controls.Grid control is one of the most commonly used containers in Silverlight. It allows you to organize its content into rows and columns as a regular grid. By default a Grid control has only one cell and everything you add goes there overlapping the previous content. In order to define multiple rows and columns to your Grid control you have to add as many <RowDefinition> and <ColumnDefinition> as you need, see the sample below.

    Xaml

    <Page
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
       <Grid x:Name="MyGrid" ShowGridLines="True">
         <Grid.RowDefinitions>
           <RowDefinition></RowDefinition>
           <RowDefinition></RowDefinition>
           <RowDefinition></RowDefinition>
         </Grid.RowDefinitions>
         <Grid.ColumnDefinitions>
           <ColumnDefinition></ColumnDefinition>
           <ColumnDefinition></ColumnDefinition>
           <ColumnDefinition></ColumnDefinition>
         </Grid.ColumnDefinitions>
       </Grid>
    </Page>

     

    C#

    Grid MyGrid = new Grid();
     
    RowDefinition row1 = new RowDefinition();
    RowDefinition row2 = new RowDefinition();
    RowDefinition row3 = new RowDefinition();
    MyGrid.RowDefinitions.Add( row1 );
    MyGrid.RowDefinitions.Add( row2 );
    MyGrid.RowDefinitions.Add( row3 );
    ColumnDefinition col1 = new ColumnDefinition();
    ColumnDefinition col2 = new ColumnDefinition();
    ColumnDefinition col3 = new ColumnDefinition();
    MyGrid.ColumnDefinitions.Add( col1 );
    MyGrid.ColumnDefinitions.Add( col2 );
    MyGrid.ColumnDefinitions.Add( col3 );

    As you can see we have added 3 rows and 3 columns definitions in our Grid control.

    Share


  • 5 comments  /  posted by  Denislav Savkov  on  Sep 02, 2008 (more than a year ago)
    Tags: Silverlight , Grid


    Let's imagine we would like to add a button at a specific cell (row:column) in our grid control. How this could be done? How to define the row and column of that cell? Well, the answer is pretty easy - you just have to define values for the two attached properties Grid.Row and Grid.Column inside the control you wish to add. Like this:

    Xaml

    <Grid x:Name="MyGrid" ShowGridLines="True">
       <Grid.RowDefinitions>
          <RowDefinition></RowDefinition>
          <RowDefinition></RowDefinition>
       </Grid.RowDefinitions>
       <Grid.ColumnDefinitions>
          <ColumnDefinition></ColumnDefinition>
          <ColumnDefinition></ColumnDefinition>
       </Grid.ColumnDefinitions>
       <Button x:Name="MyButton" Content="Button at Cell(R:0, C:1)" Grid.Row="0" Grid.Column="1"></Button>
    </Grid>
    Share
  • 4 comments  /  posted by  Denislav Savkov  on  Sep 02, 2008 (more than a year ago)

    Sometimes you might need to handle the application exit event of a Silverlight application in order to logout, clear or perform any other operations required by your internal logic. Exactly for this reason there is an Exit event located in your project application class that directly derives from System.Windows.Application. In most cases the name of this class is App and you can access its Exit event like this:

    C#

       App.Current.Exit += new EventHandler( ApplicationExit );

    That's it!

    Share
  • 0 comments  /  posted by  Denislav Savkov  on  Sep 02, 2008 (more than a year ago)

    Sometimes you might need to handle the application startup event of a Silverlight application in order to initialize or perform any other operations required by your internal logic. Exactly for this reason there is an Startup event located in your project application class that directly derives from System.Windows.Application. In most cases the name of this class is App and you can access its Startup event like this:

    C#

       App.Current.Startup += new StartupEventHandler( ApplicationStartup );

    Keep in mind that this event is fired only once in the absolute start of the application, when no controls have been still instantiated.

    Share
  • 7 comments  /  posted by  Ivan Dragoev  on  Sep 02, 2008 (more than a year ago)
    Tags: Silverlight , ScrollViewer , ItemsControl

    To add scrollbars to ItemsControl you have to modify the control template and to add ScrollViewer for the ItemsPresenter.

    Xaml

    <ItemsControl >
    <
    ItemsControl.Template>
    <
    ControlTemplate>
    <
    ScrollViewer x:Name="ScrollViewer" Padding="{TemplateBinding Padding}">
    <
    ItemsPresenter />
    </
    ScrollViewer>
    </
    ControlTemplate>
    </
    ItemsControl.Template>
    </
    ItemsControl>

     

    Share
  • 0 comments  /  posted by  Denislav Savkov  on  Sep 02, 2008 (more than a year ago)
    Tags: Silverlight , cursor

    Control's cursors can be changed very easily in Silverlight. All controls that derive from System.Windows.FrameworkElement have Cursor property that can be set from Xaml or the code behind. The value for that property should be one of the predefined cursors such as Hand, Wait, Arrow, IBeam etc. declared in the static class System.Windows.Input.Cursors.

    Xaml

       <Button x:Name="pushMeButton" Content="Push me now!" Cursor="Hand"></Button>
     
    C#
       pushMeButton.Cursor = Cursors.Hand;
    That's it!
    Share
  • 0 comments  /  posted by  Denislav Savkov  on  Sep 01, 2008 (more than a year ago)

    In order to disassemble the source code of the Silverlight assemblies, you need to download and install the .NET Reflector from http://www.red-gate.com/products/reflector. This useful application was originally developed by Lutz Roeder and later acquired by Red Gate.
    You just have to start the reflector and open the Silverlight assemblies located in C:\Program Files\Microsoft Silverlight\<version>.

    That's it!

    Share
  • 0 comments  /  posted by  Denislav Savkov  on  Sep 01, 2008 (more than a year ago)

    User control static resources are used to declare instances of objects that should be alive and reusable throughout the whole life of a user control. When you declare a static resource in the Xaml of a user control Visual Studio actually declares this instance as an internal class field of that user control, thus making it directly accessible only in your code behind. Usually such things like styles and templates are declared as static resources and as result they can be used multiple times from within your control.

    Share
  • 0 comments  /  posted by  Denislav Savkov  on  Sep 01, 2008 (more than a year ago)

    Application static resources are used to declare instances of objects that should be alive and reusable throughout the whole life of an application. Usually as static resources are declared such things as styles, templates etc. Thus they can be used multiple times from multiple places within your Silverlight application.

    Xaml

       <Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
                    x:Class="SnackBites.App"
                    xmlns:local="clr-namespace:Bomb">
           <Application.Resources>
                   <Style x:Name="simpleStyle" TargetType="Button">
                       <Setter Property="Content" Value="Static style content"></Setter>
                   </Style>
           </Application.Resources>
       </Application>
    Note that for each resource you declare x:Name or x:Key should be specified!
    That's it!
    Share
  • 2 comments  /  posted by  Denislav Savkov  on  Sep 01, 2008 (more than a year ago)

    Static resources are declared in Silverlight with the only idea to be reused from multiple places in your code. For example, when you declare a style as a static resource on application level, then this style can be referenced and applied to any control in that application. The syntax when you reference a declared static resource is as follows: {StaticResource resourceName} - where resourceName should be replaced with the name of the declared resource. See the example below:

    Xaml

       <Button Style="{StaticResource simpleStyle}" x:Name="pushMeButton" Cursor="Hand"></Button>

    As you can see the Button's Style property is set to be equal to {StaticResource simpleStyle}.

    Share

Page