я сделал что-то вроде того, что вы ищете с регулярным IValueConverter
:
public class BooleanConverter<T> : DependencyObject, IValueConverter {
public static DependencyProperty FalseProperty =
DependencyProperty.Register("False", typeof(T), typeof(BooleanConverter<T>), new PropertyMetadata(default(T)));
public static DependencyProperty TrueProperty =
DependencyProperty.Register("True", typeof(T), typeof(BooleanConverter<T>), new PropertyMetadata(default(T)));
public T False {
get { return (T) GetValue(FalseProperty); }
set { SetValue(FalseProperty, value); }
}
public T True {
get { return (T) GetValue(TrueProperty); }
set { SetValue(TrueProperty, value); }
}
public BooleanConverter(T trueValue, T falseValue) {
True = trueValue;
False = falseValue;
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
bool b = false;
if (value is bool) b = (bool) value;
else if (value is string) b = bool.Parse(value as string);
return b ? True : False;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
return value is T && EqualityComparer<T>.Default.Equals((T) value, True);
}
}
Тогда я реализовал множество новых классов, которые происходят от общего типа. Например:
[ValueConversion(typeof(bool), typeof(Brush))]
public class BooleanToBrushConverter : BooleanConverter<Brush> {
public BooleanToBrushConverter() :
base(new SolidColorBrush(Colors.Black), new SolidColorBrush(Colors.Red)) { }
}
Вы можете, вероятно, сделать что-то подобное для IMultiValueConverter
классов. Свойства True
& False
все равно будут присутствовать, это просто, что логика для принятия решения о том, какое значение свойства возвращать включает логически AND или ORing значения в переданном массиве.
Что-то вроде этого:
public class AndConverter<T> : DependencyObject, : DependencyObject, IMultiValueConverter{
public static DependencyProperty FalseProperty =
DependencyProperty.Register("False", typeof(T), typeof(AndConverter<T>), new PropertyMetadata(default(T)));
public static DependencyProperty TrueProperty =
DependencyProperty.Register("True", typeof(T), typeof(AndConverter<T>), new PropertyMetadata(default(T)));
public T False {
get { return (T) GetValue(FalseProperty); }
set { SetValue(FalseProperty, value); }
}
public T True {
get { return (T) GetValue(TrueProperty); }
set { SetValue(TrueProperty, value); }
}
public AndConverter(T trueValue, T falseValue) {
True = trueValue;
False = falseValue;
}
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
return (<Your logic to compute the result goes here>) ? True : False;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) {
// . . .
}
}
Тогда вы можете создать свой класс для преобразования Видимость:
[ValueConversion(typeof(bool), typeof(Visibility))]
public class AndVisibilityConverter : AndConverter<Visibility> {
public AndVisibilityConverter() :
base(Visibility.Visible, Visibility.Hidden) { }
}
Вам нужно преобразовать в строку? Поскольку BooleanToVisibilityConverter не принимает строковый ввод, который не очень помогает. Зачем вам нужно преобразовать bool в строку? - он имеет метод ToString(). – Paparazzi
WPF не просто автоматически вызывает .ToString(). – Nick
Действительно? Итак, вы пытались привязать TextBox к логическому? – Paparazzi