如何将多个参数传递给WPF MarkupExtension中的params数组?

时间:2009-02-09 20:15:37

标签: wpf

我需要为我的WPF应用程序创建一个MarkupExtension,它需要能够在国际化字符串中使用任意数量的参数来替换令牌。我们的国际化代码使用params数组来获取这些参数。由于我希望这些可以从XAML传递,有没有办法指定这些参数而无需在XAML中显式创建x:数组?

2 个答案:

答案 0 :(得分:2)

我认为你必须使用x:Array,我不知道任何其他方式(甚至不能想到任何其他方式与标记扩展{}语法兼容)。

答案 1 :(得分:0)

有时它不漂亮,但蛮力带来了一天...

来自来源:

[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;
}

}