当前位置: 首页>>代码示例>>C#>>正文


C# IValueConverter.ConvertBack方法代码示例

本文整理汇总了C#中IValueConverter.ConvertBack方法的典型用法代码示例。如果您正苦于以下问题:C# IValueConverter.ConvertBack方法的具体用法?C# IValueConverter.ConvertBack怎么用?C# IValueConverter.ConvertBack使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IValueConverter的用法示例。


在下文中一共展示了IValueConverter.ConvertBack方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ConvertBackWithNullParameter_ShouldThrowArgumentNullException

 public void ConvertBackWithNullParameter_ShouldThrowArgumentNullException(IValueConverter converter)
 {
     try
     {
         result = converter.ConvertBack(null, null, null, "");
         Assert.Fail("An exception should have been thrown");
     }
     catch (ArgumentNullException exception)
     {
         StringAssert.Contains(exception.Message, "");
     }
     catch (Exception e)
     {
         Assert.Fail("The wrong exception has been thrown");
     }
 }
开发者ID:mhebestadt,项目名称:CateringKingCalculator,代码行数:16,代码来源:ConverterTestBase.cs

示例2: ConvertBackHelper

        private object ConvertBackHelper(IValueConverter converter,
                                         object value,
                                         Type sourceType,
                                         object parameter,
                                         CultureInfo culture)
        {
            Invariant.Assert(converter != 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
            {
                convertedValue = converter.ConvertBack(value, sourceType, 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
                ex = CriticalExceptions.Unwrap(ex);
                if (CriticalExceptions.IsCriticalApplicationException(ex))
                    throw;

                if (TraceData.IsEnabled)
                {
                    TraceData.Trace(TraceEventType.Error,
                        TraceData.BadConverterForUpdate(
                            AvTrace.ToStringHelper(Value),
                            AvTrace.TypeName(value)),
                        this, ex);
                }

                ProcessException(ex, ValidatesOnExceptions);
                convertedValue = DependencyProperty.UnsetValue;
            }
            catch // non CLS compliant exception
            {
                if (TraceData.IsEnabled)
                {
                    TraceData.Trace(TraceEventType.Error,
                        TraceData.BadConverterForUpdate(
                            AvTrace.ToStringHelper(Value),
                            AvTrace.TypeName(value)),
                        this);
                }
                convertedValue = DependencyProperty.UnsetValue;
            }

            #pragma warning restore 56500
            #pragma warning restore 1634, 1691

            return convertedValue;
        }
开发者ID:nlh774,项目名称:DotNetReferenceSource,代码行数:63,代码来源:BindingExpression.cs

示例3: CheckIfPropertyEditedAndUpdate

        /// <summary>
        /// Checks to see if a given value matches a property's original value,
        /// and updates the ability to commit the edit if not.
        /// </summary>
        /// <param name="propertyName">The name of the property to check against.</param>
        /// <param name="value">The value to check against.</param>
        /// <param name="doConversion">Whether or not to convert the value to the original type.</param>
        /// <param name="originalType">The original type.</param>
        /// <param name="valueConverter">The IValueConverter to use for conversion.</param>
        /// <param name="converterParameter">The converter parameter.</param>
        /// <param name="converterCulture">The converter culture.</param>
        private void CheckIfPropertyEditedAndUpdate(
            string propertyName,
            object value,
            bool doConversion,
            Type originalType,
            IValueConverter valueConverter,
            object converterParameter,
            CultureInfo converterCulture)
        {
            object originalValue;

            if (!this._editablePropertiesOriginalValues.TryGetValue(propertyName, out originalValue))
            {
                return;
            }

            if (doConversion && value != null)
            {
                // Attempt to convert the value provided to the type of the original type.
                // If the conversion fails, just keep it as is.
                try
                {
                    if (valueConverter != null)
                    {
                        value = valueConverter.ConvertBack(value, originalType, converterParameter, converterCulture);
                    }
                    else
                    {
                        value = Convert.ChangeType(value, originalType, converterCulture);
                    }
                }
                catch (InvalidCastException)
                {
                }
                catch (FormatException)
                {
                }
                catch (OverflowException)
                {
                }
            }

            bool valueEqualsOriginal =
                (value == null && originalValue == null) ||
                (value != null && originalValue != null && value.Equals(originalValue));

            if (valueEqualsOriginal &&
                this._editedProperties.Contains(propertyName))
            {
                this._editedProperties.Remove(propertyName);
                SetAllCanPropertiesAndUpdate(this, false /* onlyUpdateStates */);
            }
            else if (!valueEqualsOriginal &&
                !this._editedProperties.Contains(propertyName))
            {
                this._editedProperties.Add(propertyName);
                SetAllCanPropertiesAndUpdate(this, false /* onlyUpdateStates */);
            }
        }
开发者ID:modulexcite,项目名称:SilverlightToolkit,代码行数:70,代码来源:DataForm.cs


注:本文中的IValueConverter.ConvertBack方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。