Silverlight 4: Formatting Strings Originating from DataBinding

Handling Null values returned by the Bound object.
Using the ‘TargetNullValue=Value’ as an additional parameter in the Binding statement {} you can catch instances where the returned data is null and in its place use the specified value.
Example: <TextBlock x:Name="HandleNullsExample" Text="{Binding Cost, TargetNullValue=None}”>
Formatting Money/Currency
Using the ‘StringFormat=C’ as an additional parameter in the Binding statement {} you can format the returned data as a currency.
Example: <TextBlock x:Name="CurrencyExample" Text="{Binding Cost, StringFormat=C}”>
Formatting Dates
Using ‘StringFormat=MM/dd/yyyy’ as an additional parameter in the Binding statement {} you can format the returned data as a date.
Example: <TextBlock x:Name="BirthDateValueTextBlock" Text="{Binding BirthDate, StringFormat=MM/dd/yyyy}”>
Fallback Values when Binding fails
Using ‘FallbackValue=Value’ as an additional parameter in the Binding statement {} you can pass in a value that will be used when the data binding fails.
Example: <TextBlock x:Name="ManagerOfValueTextBlock" Text="{Binding ManagerOf, FallbackValue=NA}”>

Silverlight: Passing InitParams to your Application from the Page

To pass initialization parameters (InitParams) to your Silverlight application at its startup you can simply add a new  ‘initParams’ element to the Silverlight Object markup within your page. The ‘initParams’ element contains for its value a hash of key value pairs you wish to pass into the application in the for “key=value”.
Example:

<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
    <param name="source" value="ClientBin/JoxStrap.Silverlight.Home.xap"/>
    <param name="onError" value="onSilverlightError" />
    <param name="background" value="white" />
    <param name="minRuntimeVersion" value="3.0.40624.0" />
    <param name="autoUpgrade" value="true" />
    <param name="initParams" value="baseUri=http://local.joxstrap.com" />
    <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40624.0" style="text-decoration:none">
      <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style:none"/>
    </a>
     </object><iframe id="_sl_historyFrame" style="visibility:hidden;height:0px;width:0px;border:0px"></iframe></div>

After adding the ‘initParam’ element to the object markup you then read in these parameters in the Application_Startup method located in your App.xaml.cs file.

private void Application_Startup(object sender, StartupEventArgs e)
        {
            StartUpParameters startup = StartUpParameters.Instance;

            try
            {
                string uriString = e.InitParams["baseUri"];
                if (!string.IsNullOrEmpty(uriString))
                {
                    Uri baseUri = new Uri(uriString);
                    startup.BaseUri = baseUri;
                }
            }
            catch
            {
                startup.BaseUri = null;
            }

            RootVisual = new MainPage();
        }

Silverlight 3: HTML dropdown menus fall behind Silverlight

If when you hover over the dropdown menus in your website and find that they are disappearing behind your Silverlight media elements in the page making it impossible to click on the hidden menu items, there is a simple solution. You need to activate the ‘windowless’ mode for all Silverlight media elements which are within reach of your dropdowns.
To do this just add the following parameter to your object markup for the Silverlight element.

<param name="windowless" value="true"/>

Your object markup should now look something like this:

 <object data="data:application/x-silverlight-2," type="application/x-silverlight-2"
            width="950px" height="665px">
            <param name="windowless" value="true"/>
            <param name="source" value="URI_TO_SILVERLIGHT_FILE" />
            <param name="onError" value="onSilverlightError" />
            <param name="minRuntimeVersion" value="3.0.40624.0" />
            <param name="autoUpgrade" value="true" />
            <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40624.0" style="text-decoration: none">
                <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight"
                    style="border-style: none" />
            </a>
        </object>
        <iframe id="_sl_historyFrame" style="visibility: hidden; height: 0px; width: 0px; border: 0px"></iframe>

Silverlight 3: Using Built-in Behaviors and Triggers to Reduce Code

With the addition of Behaviors in Silverlight 3 we have the opportunity to reduce a lot of the code we write in order to create interactivity within our application. Expressions Blend 3 comes with several built-in behaviors right at your fingertips. You can find them in the Assets panel (left side) under the Behaviors group.

  • ChangePropertyAction
  • ControlStoryboardAction
  • FluidMoveBehavior
  • GoToStateAction
  • HyperlinkAction
  • MouseDragElementBehavior
  • PlaySoundAction
  • RemoveElementAction

In this example we will be building a simplistic sound playing app using the ChangePropertyAction as well as the PlaySoundAction Behaviors. By utilizing these 2 built-in Behaviors we can completely eliminate writing a single line of code.

Silverlight 3: Transparent Background Controls

The first time you add a Silverlight 3 control which is supposed to have transparent portions to let your site design shine through you might notice that those sections are black and not the anticipated transparency.
Silverlight controls by default have a window that surrounds them and is set to black. Transparency of course takes a lot of resources to accomplish and as you would guess it is turned off by default. In order to get the desired effect you need to alter an existing parameter and add an additional one.
Existing Parameter: <param name="background" value="white” />
Changed To: <param name="background" value="transparent” />
Additional Parameter: <param name="windowless" value="true” />