• Code
  • Tags
  • Users
  • Titles
  • Log in
  • Feedback
  • FAQ
Share Code
Welcome to ForkCan.com

ForkCan is all about sharing code in a social way.

Discuss, debate or argue with other devs about their or your own code.

Give other devs feedback or make a Fork (Make a better version of a shared code).

Rate the code, if you use the code mark it as used so others can see if the shared code is used by someone.

Help each other to be better devs and to be more productive.


Features not working yet:

Flag a post


QR Code

Tiny Url

http://4kcan.com/s/MjI5

Resource.Key (ResourceKey) for Silverlight 4

Implements a dependency property for Silverlight 4 that enables Resource.Key and Resource.Tooltip.

0
527 0 0 0 0 0

Here is a implementation that simplifies the way you work with resources for localization in Silverlight 4. Please see the following blogpost for more details on how it works: http://sondreb.com/blog/post/Simplifying-Text-Resources-in-Silverlight.aspx

namespace SimplifyResources
{
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System;

    public static class Resource
    {
        /// <summary>
        /// A dependency property for attaching a resource key to the element.
        /// </summary>
        public static readonly DependencyProperty KeyProperty =
            DependencyProperty.RegisterAttached(
                "Key",
                typeof(string),
                typeof(string),
                new PropertyMetadata(OnKeyChanged)
                );

        public static string GetKey(DependencyObject obj)
        {
            return (string)obj.GetValue(KeyProperty);
        }

        public static void SetKey(DependencyObject obj, string value)
        {
            obj.SetValue(KeyProperty, value);
        }

        /// <summary>
        /// A dependency property for attaching a resource key to the element used in the tooltip.
        /// </summary>
        public static readonly DependencyProperty TooltipProperty =
            DependencyProperty.RegisterAttached(
                "Tooltip",
                typeof(string),
                typeof(string),
                new PropertyMetadata(OnTooltipChanged)
                );

        public static string GetTooltip(DependencyObject obj)
        {
            return (string)obj.GetValue(TooltipProperty);
        }

        public static void SetTooltip(DependencyObject obj, string value)
        {
            obj.SetValue(TooltipProperty, value);
        }

        private static void OnKeyChanged(DependencyObject targetLocation, DependencyPropertyChangedEventArgs args)
        {
            SetupBinding(targetLocation, args, false);
        }

        private static void OnTooltipChanged(DependencyObject targetLocation, DependencyPropertyChangedEventArgs args)
        {
            SetupBinding(targetLocation, args, true);
        }

        private static void SetupBinding(DependencyObject targetLocation, DependencyPropertyChangedEventArgs args, bool isTooltip)
        {
            if (args.OldValue == args.NewValue) return;

            FrameworkElement element = targetLocation as FrameworkElement;

            if (element == null)
            {
                throw new Exception("Resource.Key can only be set on framework elements.");
            }

            // Load the resources source from the application's StaticResources collection.
            // This will only work if you have added it to your App.xaml.
            var dataSource = Application.Current.Resources["Resources"];

            Binding binding = new Binding((string)args.NewValue);
            binding.Source = dataSource;

            if (isTooltip)
            {
                element.SetBinding(ToolTipService.ToolTipProperty, binding);
            }
            else
            {
                element.SetBinding(FindDependencyProperty(element), binding);
            }
        }

        private static DependencyProperty FindDependencyProperty(DependencyObject control)
        {
            DependencyProperty property = null;

            if (control is Button)
            {
                property = Button.ContentProperty;
            }
            else if (control is TextBlock)
            {
                property = TextBlock.TextProperty;
            }
            else if (control is TextBox)
            {
                property = TextBox.TextProperty;
            }
            else if (control is CheckBox)
            {
                property = CheckBox.ContentProperty;
            }

            return property;
        }
    }
}

Here follows an example on use:

<Button res:Resource.Key="About" res:Resource.Tooltip="AboutTooltip" />

Share: twitter | facebook   Action: used | fork | flag

localization

Mark 'localization' tag as 'like'

Mark 'localization' tag as 'ignore'

resources

Mark 'resources' tag as 'like'

Mark 'resources' tag as 'ignore'

silverlight

Mark 'silverlight' tag as 'like'

Mark 'silverlight' tag as 'ignore'


 @sondreb "Code Contributor"
409
August 15, 2010 4:28 PM

Fork

 Resource.Key (ResourceKey) for Silverlight 4 -  @sondreb Sunday 15, 2010 4:28 PM


0 Feedback


You must log in before you can give any feedback


0 Discussion(s)

Newest Oldest

You must log in before you can post a comment

Squeed
Made by: Fredrik Normén 2010