Binding ConvererParameter
WPF中ConverterParameter不可以绑定,可以通过如下扩展来实现ConverterParameter的Binding:
1.自定义ConverterBindableBinding和MultiValueConverterAdapter:
namespace TTSControl.UtilityClasses
{
public class ConverterBindableBinding : MarkupExtension
{
public Binding Binding { get; set; }
public IValueConverter Converter { get; set; }
public Binding ConverterParameterBinding { get; set; }
public Binding ConverterBinding { get; set; }
public override object ProvideValue(IServiceProvider serviceProvider)
{
MultiBinding multiBinding = new MultiBinding();
multiBinding.Bindings.Add(Binding);
multiBinding.Bindings.Add(ConverterParameterBinding);
if (ConverterBinding != null) multiBinding.Bindings.Add(ConverterBinding);
MultiValueConverterAdapter adapter = new MultiValueConverterAdapter();
adapter.Converter = Converter;
multiBinding.Converter = adapter;
return multiBinding.ProvideValue(serviceProvider);
}
}
}
namespace TTSControl.UtilityClasses
{
[ContentProperty("Converter")]
public class MultiValueConverterAdapter : IMultiValueConverter
{
public IValueConverter Converter { get; set; }
#region IMultiValueConverter Members
private object lastParameter;
private IValueConverter lastConverter;
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
lastConverter = Converter;
if (values.Length > 1) lastParameter = values[1];
if (values.Length > 2) lastConverter = (IValueConverter)values[2];
if (Converter == null) return values[0];
return Converter.Convert(values[0], targetType, lastParameter, culture);
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
if (lastConverter == null) return new object[] { value };
return new object[] { lastConverter.ConvertBack(value, targetTypes[0], lastParameter, culture) };
}
#endregion
}
}
2。在xaml中绑定:
xmlns:uti="clr-namespace:TTSControl.UtilityClasses"
Binding="{Binding PermissionKey}" Converter="{StaticResource permissionValueConverter}"ConverterParameterBinding="{Binding UserName}" />
3。在自定义Converter类中获取绑定数据:
internal class PermissionValueConverter : DependencyObject, IValueConverter, IMultiValueConverter
{
// For testing of converter properties, we derice from DependencyObject and add a dependency property.
public String UserName
{
get { return (String)GetValue(UserNameProperty); }
set { SetValue(UserNameProperty, value); }
}
// Using a DependencyProperty as the backing store for ConcatSign. This enables animation, styling, binding, etc...
public static readonly DependencyProperty UserNameProperty =
DependencyProperty.Register("UserName", typeof(String), typeof(PermissionValueConverter), new UIPropertyMetadata(null));
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string permissionKey = (string)value;
string userName= (string)parameter;
List> PermissionValues = new List>();
PermissionValues = Database.GetPermissionValue(Properties.Settings.Default.gLanguage, userName, permissionKey);
return PermissionValues;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
#region IMultiValueConverter Members
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return Convert(values[0], targetType, values[1], culture);
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
return new object[] { ConvertBack(value, targetTypes[0], parameter, culture) };
}
#endregion
}