У меня есть простой конвертер, который проверяет, равен ли объект тому параметру, который я передаю. Моя проблема в том, что параметр конвертера всегда передается как строка, а значение всегда передается как объект. Чтобы сравнить их правильно, мне нужно привести параметр к тому же типу, что и значение. Есть ли способ, которым я мог бы привести тип одного объекта к типу другого, не зная заранее ни одного типа?
public class IsObjectEqualParameterConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null && parameter == null)
return true;
if (value == null)
return false;
// Incorrectly returns False when the ConverterParameter is an integer
// Would like to try and cast parameter into whatever type value before checking equality
// Something like: return value.Equals((parameter as value.GetType()));
return value.Equals(parameter);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
И пример использования будет что-то вроде:
<Button IsEnabled="{Binding Path=Index, ConverterParameter=0, Converter={StaticResource IsObjectEqualParameterConverter}}" />