tags:

views:

100

answers:

2

I need to create a MarkupExtension for my WPF application that will need to be able to take an arbitrary number of parameters that will be used for token replacement in internationalized strings. Our internationalization code uses a params array to take these parameters. Since I want these to be passable from XAML, is there a way to specify these parameters without explicitly creating an x:Array in XAML?

A: 

I think you have to use the x:Array, I don't know any other way (and can't even think of any other way that will be compatible with the markup extension {} syntax).

Nir
You are correct. I looked into this for a long time and came up with the same answer.
Brent Schooley
A: 

Sometimes it isn't pretty, but brute force carries the day ...

[MarkupExtensionReturnType(typeof(PriorityBinding))]

public sealed class Priority:MarkupExtension {

readonly BindingBase[] bindings;

#region Constructors
public Priority(BindingBase b1) {
  bindings = new [] {b1};
}

public Priority(BindingBase b1, BindingBase b2) {
  bindings = new [] {b1, b2};
}

public Priority(BindingBase b1, BindingBase b2, BindingBase b3) {
  bindings = new [] {b1, b2, b3};
}

public Priority(BindingBase b1, BindingBase b2, BindingBase b3, 
                 BindingBase b4) {
  bindings = new [] {b1, b2, b3, b4};
}

public Priority(BindingBase b1, BindingBase b2, BindingBase b3,
                 BindingBase b4, BindingBase b5) {
  bindings = new [] {b1, b2, b3, b4, b5};
}

public Priority(BindingBase b1, BindingBase b2, BindingBase b3,
                 BindingBase b4, BindingBase b5, BindingBase b6) {
  bindings = new [] {b1, b2, b3, b4, b5, b6};
}

public Priority(BindingBase b1, BindingBase b2, BindingBase b3,
                 BindingBase b4, BindingBase b5, BindingBase b6, BindingBase b7) {
  bindings = new [] {b1, b2, b3, b4, b5, b6, b7};
}

public Priority(BindingBase b1, BindingBase b2, BindingBase b3,
                 BindingBase b4, BindingBase b5, BindingBase b6, BindingBase b7, BindingBase b8) {
  bindings = new [] {b1, b2, b3, b4, b5, b6, b7, b8};
}

#endregion

public override object ProvideValue(IServiceProvider serviceProvider) {
  var binding = new PriorityBinding();
  foreach (var item in bindings) {
    binding.Bindings.Add(item);
  }
  return binding;
}

}

John Melville