我目前正在使用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! –