Implements a dependency property for Silverlight 4 that enables Resource.Key and Resource.Tooltip.
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" />
Fork
0 Feedback
You must log in before you can give any feedback
You must log in before you can post a comment


527
0




Mark 'localization' tag as 'like'
Mark 'localization' tag as 'ignore'