Skip to content

WPF Markup Extensions in ATF

Gary edited this page Mar 2, 2015 · 1 revision

Markup extensions allow you to return an object as an attribute value in XAML. A markup extension derives from the System.Windows.Markup.MarkupExtension class. The constructors for the class can take whatever parameters are needed. The ProvideValue() method returns the object for the attribute value, so the markup extension class has complete flexibility in computing the value.

LocExtension Example

LocExtension provides an example of how an extension can provide localization services. Its main constructor takes a string to be localized:

public LocExtension(string key)

Its ProvideValue() method is very simple:

public override object ProvideValue(IServiceProvider serviceProvider)
{
	if (string.IsNullOrEmpty(Key))
		return string.Empty;

	return string.IsNullOrEmpty(Format) ? Key.Localize() : string.Format(Format, Key.Localize());
}

Using LocExtension allows you to localize text in the XAML file, and numerous XAML files in ATF use this markup extension. For instance, here are some lines from OutputView.xaml for dialogs that display text output to a user:

<MenuItem Header="{m:Loc Copy to Clipboard}" Command="ApplicationCommands.Copy"/>
<MenuItem Header="{m:Loc Clear All}" Command="{StaticResource ClearAllRef}"/>

ATF Markup Extension Classes

ATF provides a variety of markup extensions in the Sce.Atf.Wpf.Markup namespace. Here are several with a description of what the ProvideValue() method provides.

  • CommandServiceExtension: ICommandItem representing a command.
  • ConverterMarkupExtension<T>: Newly constructed converter object for a given type. ATF has many WPF converters that derive from this class.
  • EnumValuesExtension: Array of enumeration members representing the enum values of the given enumeration type.
  • GenericExtension: Concrete object of a given generic type.
  • LocExtension: Localized version of a given string.
  • MultiConverterMarkupExtension<T>: Newly constructed multiconverter object for a given type for multibinding. ATF has several WPF multiconverters that derive from this class.
  • ResourceKeyBinding: Data binding for a given resource key.
  • TypeConverterExtension: Type converter from one type to another, specifying source and target types.

Topics in this Section

Clone this wiki locally