本文整理汇总了C#中IValueConverter.Convert方法的典型用法代码示例。如果您正苦于以下问题:C# IValueConverter.Convert方法的具体用法?C# IValueConverter.Convert怎么用?C# IValueConverter.Convert使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IValueConverter
的用法示例。
在下文中一共展示了IValueConverter.Convert方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetEnumName
static string GetEnumName(FieldInfo field, Enum value, IValueConverter nameConverter, bool splitNames) {
if(nameConverter != null)
return nameConverter.Convert(value, typeof(string), null, CultureInfo.CurrentCulture) as string;
string name = DataAnnotationsAttributeHelper.GetFieldDisplayName(field) ??
#if !SILVERLIGHT
TypeDescriptor.GetConverter(value.GetType()).ConvertTo(value, typeof(string)) as string
#else
value.ToString()
#endif
;
if(splitNames)
return SplitStringHelper.SplitPascalCaseString(name);
return name;
}
示例2: ConvertWithNullParameter_ShouldThrowArgumentNullException
public void ConvertWithNullParameter_ShouldThrowArgumentNullException(IValueConverter converter)
{
try
{
result = converter.Convert(null, null, null, "");
Assert.Fail("An exception should have been thrown");
}
catch (ArgumentNullException exception)
{
StringAssert.Contains(exception.Message, "");
}
catch(Exception e)
{
Assert.Fail("Wrong exception has been thrown");
}
}
示例3: ConvertHelper
private object ConvertHelper(IValueConverter converter, object value, Type targetType, object parameter, CultureInfo culture)
{
// use the StringFormat (if appropriate) in preference to the default converter
string stringFormat = EffectiveStringFormat;
Invariant.Assert(converter != null || stringFormat != null);
// PreSharp uses message numbers that the C# compiler doesn't know about.
// Disable the C# complaints, per the PreSharp documentation.
#pragma warning disable 1634, 1691
// PreSharp complains about catching NullReference (and other) exceptions.
// It doesn't recognize that IsCritical[Application]Exception() handles these correctly.
#pragma warning disable 56500
object convertedValue = null;
try
{
if (stringFormat != null)
{
convertedValue = String.Format(culture, stringFormat, value);
}
else
{
convertedValue = converter.Convert(value, targetType, parameter, culture);
}
}
// Catch all exceptions. There is no app code on the stack,
// so the exception isn't actionable by the app.
// Yet we don't want to crash the app.
catch (Exception ex)
{
// the DefaultValueConverter can end up calling BaseUriHelper.GetBaseUri()
// which can raise SecurityException if the app does not have the right FileIO privileges
if (CriticalExceptions.IsCriticalApplicationException(ex))
throw;
if (TraceData.IsEnabled)
{
string name = String.IsNullOrEmpty(stringFormat) ? converter.GetType().Name : "StringFormat";
TraceData.Trace(TraceLevel,
TraceData.BadConverterForTransfer(
name,
AvTrace.ToStringHelper(value),
AvTrace.TypeName(value)),
this, ex);
}
convertedValue = DependencyProperty.UnsetValue;
}
catch // non CLS compliant exception
{
if (TraceData.IsEnabled)
{
TraceData.Trace(TraceLevel,
TraceData.BadConverterForTransfer(
converter.GetType().Name,
AvTrace.ToStringHelper(value),
AvTrace.TypeName(value)),
this);
}
convertedValue = DependencyProperty.UnsetValue;
}
#pragma warning restore 56500
#pragma warning restore 1634, 1691
return convertedValue;
}
示例4: Convert
public object Convert(object value, Type targetType, IValueConverter valueConverter)
{
object result = value;
if (valueConverter != null)
{
var parameter = GetConverterParameter();
result = valueConverter.Convert(value, targetType, parameter, CultureInfo.CurrentUICulture);
}
if (targetType != null)
{
var typeCode = System.Convert.GetTypeCode(value);
if (typeCode != TypeCode.Object && typeCode != TypeCode.Empty)
{
try
{
result = System.Convert.ChangeType(result, targetType);
}
catch (InvalidCastException)
{
}
}
}
return result;
}