Markup Extensions in WPF / Silverlight

Has anyone ever created their own markup extension in WPF or Silverlight? When will you ever want or want to do this? Any tips or sources on how to do this?

+6
wpf silverlight markup-extensions
source share
4 answers

Another example: Localization

Note. You cannot create custom markup extensions in silverlight.

+9
source share

Yes, it’s convenient, and I created it myself. I created a markup extension called EvalBinding that accepts a set of bindings as children and a C # evaluation string. It evaluates C # to handle values ​​from child bindings, so I don't need to create many simple TypeConverter classes.

For example, I can do this ...

<EvalBinding Eval="(this[0] > this[1] ? 'GT' : 'LTE')"> <Binding ElementName="element1" Path="Size"/> <Binding ElementName="element2" Path="Size"/> <EvalBinding> 

If it is a reference to an array of child anchor results.

For resources for implementing MarkupExtension ...

MSDN

Josh Smith Blog Entry

Rob Relyea Blog Post

+4
source share

Hooray !!

It is implemented in Silverlight 5!

And besides, now it's a common interface instead of a class !!

Check it out .

Read this as an example.

+2
source share

I know this is an old post, but I use the markup extension to standardize my validation bindings. Thus, the advantage here is small, 4 of the default values ​​that I no longer need to set, and if I want to change them in the future, I only do it here.

 using System; using System.Windows.Data; using System.Windows.Markup; namespace ITIS { /// <summary> /// Creates a normal Binding but defaults NotifyOnValidationError to True, /// ValidatesOnExceptions to True, Mode to TwoWay and /// UpdateSourceTrigger to LostFocus. /// </summary> public sealed class ValidatedBinding : MarkupExtension { public ValidatedBinding(string path) { Mode = BindingMode.TwoWay; UpdateSourceTrigger = UpdateSourceTrigger.LostFocus; Path = path; } public override object ProvideValue(IServiceProvider serviceProvider) { return new Binding(Path) { Converter = this.Converter, ConverterParameter = this.ConverterParameter, ElementName = this.ElementName, FallbackValue = this.FallbackValue, Mode = this.Mode, NotifyOnValidationError = true, StringFormat = this.StringFormat, ValidatesOnExceptions = true, UpdateSourceTrigger = this.UpdateSourceTrigger }; } public IValueConverter Converter { get; set; } public object ConverterParameter { get; set; } public string ElementName { get; set; } public object FallbackValue { get; set; } public BindingMode Mode { get; set; } public string Path { get; set; } public string StringFormat { get; set; } public UpdateSourceTrigger UpdateSourceTrigger { get; set; } } } 
0
source share

All Articles