我目前正在使用WPF上的CustomConverter
。这就像一个泛型转换。ObservableCollection <T> on xaml
读到这个blog,找到了一种简化xaml的方法。
所以该转换器看起来像这样:
public CustomConverter : MarkupExtension, IMultiValueConverter {
public ParametersCollection Parameters { get; set; }
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
if(Parameters == null)
return Binding.DoNothing;
//Convertion Logic
//...
}
public override object ProvideValue(IServiceProvider serviceProvider) {
return new CustomConverter();
}
}
public class ParametersCollection : ObservableCollection<object> {
}
而在我的XAML文件中,有以下几点:
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:primitives="clr-namespace:System;assembly=mscorlib"
xmlns:converter="clr-namespace:NS.Converters">
<Label>
<Label.Visibility>
<MultiBinding>
<converter:CustomConverter>
<converter:CustomConverter.Parameters>
<converter:ParametersCollection>
<primitives:String>Param1</primitives:String>
...
</converter:ParametersCollection>
</converter:CustomConverter.Parameters>
</converter:CustomConverter>
</MultiBinding>
<!--Bindings start here -->
</Label.Visibility>
</Label>
</UserControl>
所以debbuging代码时,该Parameters
属性为空(NULL)所以xaml不会填充集合。所以我的问题是,如何通过只使用xaml,没有C#代码来弹出参数。
你是对的,问题是变量未初始化。 – jcvegan
@jcvegan Groovy! –