Solibulo

Uninteresting things

Silverlight 3 – Tip of the month #2 - Using Data Binding with Static Resources in Silverlight

by softlion 10. October 2009 13:06

 

In his blog, Pedro wrote a post showing how to bind a property to a static resource where the resource name is given by the binded property. But his solution needs WPF libraries and does not work in Silverlight.

This is the first way to write it which comes to mind. But it doesn’t work as StaticResource does not support Binding (it is static by nature).

<Image Source="{StaticResource {Binding AvailabilityStateResourceName}}" />

 

The solution I came with is elegant. It uses a custom converter to find the resource in the application resource dictionary. The binding source must be a string containing the name of the static resource.

 

<Image Source="{Binding AvailabilityStateResourceName, Converter={StaticResource StaticResourceConverter}}" />

 

To use it, declare a static resource refering to the converter like this:

<Application ...
             xmlns:shc="clr-namespace:Softlion.Silverlight.Shared.Converters;assembly=Softlion.Silverlight.Shared" 
             >
    <Application.Resources>
        <shc:StaticResourceConverter x:Key="StaticResourceConverter" />
        ...
    </Application.Resources>
</Application>

 

And the source code of the StaticResourceConverter which is simple and straightforward:

using System;
using System.Windows.Data;
using System.Windows;

namespace Softlion.Silverlight.Shared.Converters
{
    /// <summary>
    /// Enables a binding on a static resource in the application resource dictionary.
    /// Convert from a static resource name to a resource instance in the application resource dictionary.
    /// </summary>
    public class StaticResourceConverter : IValueConverter
    {
        #region IValueConverter Members
        /// <summary>
        /// Convert from a static resource name to a resource instance in the application resource dictionary.
        /// </summary>
        /// <param name="value">must be a reference to an existing resource in the application resource dictionary</param>
        /// <param name="targetType">defined by the resource</param>
        /// <param name="parameter">not used</param>
        /// <param name="culture">not used</param>
        /// <returns>A resource in the Application.Current.Resources dictionary</returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (!(value is string))
                throw new ArgumentException("value");

            return Application.Current.Resources[value];
        }

        /// <summary>
        /// Not implemented, no use
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
        #endregion
    }
}

SL3 – Sunday reviews

by softlion 4. October 2009 10:12

Bryant Likes wrote a good post about choosing how to add a functionality to an existing control : behaviour, subclassing or attached property along with an example for each.

Behaviors vs Subclassing in Silverlight

 

Timmy Kokke wrote a detailed post about how to style a treeview.

http://geekswithblogs.net/tkokke/archive/2009/06/29/styling-a-treeview-in-silverlight-and-blend-3.aspx

Silverlight 3 - Tip of the month #1 – Binding Color property

by softlion 4. October 2009 08:04

In XAML you can not bind a property of type Color directly. I’m not sure why. Maybe because it is a structure (ie: value type).

You can bind a Brush instead :

 

[DataMember]
internal Color color;
public Brush DisplayColorBrush { get { return new SolidColorBrush(color); } }

 

Binding to a static color resource is working though:

<TextBlock.Foreground>
    <SolidColorBrush Color="{StaticResource UserOffersInfoColor}"/>
</TextBlock.Foreground>

 

<Application.Resources>
    <Color x:Key="UserOffersInfoColor">#FFE97F26</Color>
</Application.Resources>

 

Binding a Text property in a Run tag of a TextBlock tag doesn’t work either. You must bind to the Text property of the TextBlock tag instead.

<TextBlock Margin="6,3" FontWeight="Bold" FontSize="12">
    <Run Text="Chips: "/>
    <Run Text="{Binding ChipsRemaining}"/>
</TextBlock>