Code:
/ Net / Net / 3.5.50727.3053 / DEVDIV / depot / DevDiv / releases / Orcas / SP / wpf / src / Framework / System / Windows / Documents / ZoomPercentageConverter.cs / 1 / ZoomPercentageConverter.cs
//----------------------------------------------------------------------------
//
//
// Copyright (C) Microsoft Corporation. All rights reserved.
//
//
//
// Description: Contains the ZoomPercentageConverter: TypeConverter for the
// ZoomPercentage property of DocumentViewer.
//
// History:
// 01/11/2005 - JeremyNS - Created
//
//---------------------------------------------------------------------------
// Used to support the warnings disabled below
#pragma warning disable 1634, 1691
using System;
using System.Globalization;
using System.Windows.Data;
namespace System.Windows.Documents
{
///
/// ValueConverter for DocumentViewer's ZoomPercentage property
///
public sealed class ZoomPercentageConverter : IValueConverter
{
//-----------------------------------------------------
//
// Constructors
//
//-----------------------------------------------------
///
/// Instantiates a new instance of a ZoomPercentageConverter
///
public ZoomPercentageConverter() {}
//------------------------------------------------------
//
// Public Methods
//
//-----------------------------------------------------
///
/// Convert a value. Called when moving a value from ZoomPercentage to UI.
///
/// value produced by the ZoomPercentage property
/// target type
/// converter parameter
/// culture information
///
/// Converted value.
///
/// System.Windows.DependencyProperty.UnsetValue may be returned to indicate that
/// the converter produced no value and that the fallback (if available)
/// or default value should be used instead.
///
/// Binding.DoNothing may be returned to indicate that the binding
/// should not transfer the value or use the fallback or default value.
///
///
/// The data binding engine does not catch exceptions thrown by a user-supplied
/// converter. Thus any exception thrown by Convert, or thrown by methods
/// it calls and not caught by the Convert, will be treated as a runtime error
/// (i.e. a crash). Convert should handle anticipated problems by returning
/// DependencyProperty.UnsetValue.
///
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
// Check if the targetType has been defined correctly.
if (targetType == null)
{
return DependencyProperty.UnsetValue;
}
// Ensure that the value given is a double.
if (value != null
&& value is double)
{
double percent = (double)value;
// If string requested, format string.
// If object is requested, then return a formated string. This covers cases
// similar to ButtonBase.CommandParameter, etc.
if ((targetType == typeof(string)) || (targetType == typeof(object)))
{
// Check that value is a valid double.
if ((double.IsNaN(percent)) || (double.IsInfinity(percent)))
{
return DependencyProperty.UnsetValue;
}
else
{
// Ensure output string is formatted to current globalization standards.
return String.Format(CultureInfo.CurrentCulture,
SR.Get(SRID.ZoomPercentageConverterStringFormat), percent);
}
}
// If double requested, return direct value.
else if (targetType == typeof(double))
{
return percent;
}
}
return DependencyProperty.UnsetValue;
}
///
/// Convert back a value. Called when moving a value into a ZoomPercentage.
///
/// value, as produced by target
/// target type
/// converter parameter
/// culture information
///
/// Converted back value.
///
/// Binding.DoNothing may be returned to indicate that no value
/// should be set on the source property.
///
/// System.Windows.DependencyProperty.UnsetValue may be returned to indicate
/// that the converter is unable to provide a value for the source
/// property, and no value will be set to it.
///
///
/// The data binding engine does not catch exceptions thrown by a user-supplied
/// converter. Thus any exception thrown by ConvertBack, or thrown by methods
/// it calls and not caught by the ConvertBack, will be treated as a runtime error
/// (i.e. a crash). ConvertBack should handle anticipated problems by returning
/// DependencyProperty.UnsetValue.
///
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if ((targetType == typeof(double)) && (value != null))
{
double zoomValue = 0.0;
bool isValidArg = false;
// If value an int, then cast
if (value is int)
{
zoomValue = (double)(int)value;
isValidArg = true;
}
// If value is a double, then cast
else if (value is double)
{
zoomValue = (double)value;
isValidArg = true;
}
// If value is a string, then parse
else if (value is string)
{
try
{
// Remove whitespace on either end of the string.
string zoomString = (string)value;
if ((culture != null) && !String.IsNullOrEmpty(zoomString))
{
zoomString = ((string)value).Trim();
// If this is not a neutral culture attempt to remove the percent symbol.
if ((!culture.IsNeutralCulture) && (zoomString.Length > 0) && (culture.NumberFormat != null))
{
// This will strip the percent sign (if it exists) depending on the culture information.
switch (culture.NumberFormat.PercentPositivePattern)
{
case 0: // n %
case 1: // n%
// Remove the last character if it is a percent sign
if (zoomString.Length - 1 == zoomString.LastIndexOf(
culture.NumberFormat.PercentSymbol,
StringComparison.CurrentCultureIgnoreCase))
{
zoomString = zoomString.Substring(0, zoomString.Length - 1);
}
break;
case 2: // %n
// Remove the first character if it is a percent sign.
if (0 == zoomString.IndexOf(
culture.NumberFormat.PercentSymbol,
StringComparison.CurrentCultureIgnoreCase))
{
zoomString = zoomString.Substring(1);
}
break;
}
}
// If this conversion throws then the string wasn't a valid zoom value.
zoomValue = System.Convert.ToDouble(zoomString, culture);
isValidArg = true;
}
}
// Allow empty catch statements.
#pragma warning disable 56502
// Catch only the expected parse exceptions
catch (ArgumentOutOfRangeException) { }
catch (ArgumentNullException) { }
catch (FormatException) { }
catch (OverflowException) { }
// Disallow empty catch statements.
#pragma warning restore 56502
}
// Argument wasn't a valid percent, set error value.
if (!isValidArg)
{
return DependencyProperty.UnsetValue;
}
return zoomValue;
}
// Requested type is not supported.
else
{
return DependencyProperty.UnsetValue;
}
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
//----------------------------------------------------------------------------
//
//
// Copyright (C) Microsoft Corporation. All rights reserved.
//
//
//
// Description: Contains the ZoomPercentageConverter: TypeConverter for the
// ZoomPercentage property of DocumentViewer.
//
// History:
// 01/11/2005 - JeremyNS - Created
//
//---------------------------------------------------------------------------
// Used to support the warnings disabled below
#pragma warning disable 1634, 1691
using System;
using System.Globalization;
using System.Windows.Data;
namespace System.Windows.Documents
{
///
/// ValueConverter for DocumentViewer's ZoomPercentage property
///
public sealed class ZoomPercentageConverter : IValueConverter
{
//-----------------------------------------------------
//
// Constructors
//
//-----------------------------------------------------
///
/// Instantiates a new instance of a ZoomPercentageConverter
///
public ZoomPercentageConverter() {}
//------------------------------------------------------
//
// Public Methods
//
//-----------------------------------------------------
///
/// Convert a value. Called when moving a value from ZoomPercentage to UI.
///
/// value produced by the ZoomPercentage property
/// target type
/// converter parameter
/// culture information
///
/// Converted value.
///
/// System.Windows.DependencyProperty.UnsetValue may be returned to indicate that
/// the converter produced no value and that the fallback (if available)
/// or default value should be used instead.
///
/// Binding.DoNothing may be returned to indicate that the binding
/// should not transfer the value or use the fallback or default value.
///
///
/// The data binding engine does not catch exceptions thrown by a user-supplied
/// converter. Thus any exception thrown by Convert, or thrown by methods
/// it calls and not caught by the Convert, will be treated as a runtime error
/// (i.e. a crash). Convert should handle anticipated problems by returning
/// DependencyProperty.UnsetValue.
///
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
// Check if the targetType has been defined correctly.
if (targetType == null)
{
return DependencyProperty.UnsetValue;
}
// Ensure that the value given is a double.
if (value != null
&& value is double)
{
double percent = (double)value;
// If string requested, format string.
// If object is requested, then return a formated string. This covers cases
// similar to ButtonBase.CommandParameter, etc.
if ((targetType == typeof(string)) || (targetType == typeof(object)))
{
// Check that value is a valid double.
if ((double.IsNaN(percent)) || (double.IsInfinity(percent)))
{
return DependencyProperty.UnsetValue;
}
else
{
// Ensure output string is formatted to current globalization standards.
return String.Format(CultureInfo.CurrentCulture,
SR.Get(SRID.ZoomPercentageConverterStringFormat), percent);
}
}
// If double requested, return direct value.
else if (targetType == typeof(double))
{
return percent;
}
}
return DependencyProperty.UnsetValue;
}
///
/// Convert back a value. Called when moving a value into a ZoomPercentage.
///
/// value, as produced by target
/// target type
/// converter parameter
/// culture information
///
/// Converted back value.
///
/// Binding.DoNothing may be returned to indicate that no value
/// should be set on the source property.
///
/// System.Windows.DependencyProperty.UnsetValue may be returned to indicate
/// that the converter is unable to provide a value for the source
/// property, and no value will be set to it.
///
///
/// The data binding engine does not catch exceptions thrown by a user-supplied
/// converter. Thus any exception thrown by ConvertBack, or thrown by methods
/// it calls and not caught by the ConvertBack, will be treated as a runtime error
/// (i.e. a crash). ConvertBack should handle anticipated problems by returning
/// DependencyProperty.UnsetValue.
///
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if ((targetType == typeof(double)) && (value != null))
{
double zoomValue = 0.0;
bool isValidArg = false;
// If value an int, then cast
if (value is int)
{
zoomValue = (double)(int)value;
isValidArg = true;
}
// If value is a double, then cast
else if (value is double)
{
zoomValue = (double)value;
isValidArg = true;
}
// If value is a string, then parse
else if (value is string)
{
try
{
// Remove whitespace on either end of the string.
string zoomString = (string)value;
if ((culture != null) && !String.IsNullOrEmpty(zoomString))
{
zoomString = ((string)value).Trim();
// If this is not a neutral culture attempt to remove the percent symbol.
if ((!culture.IsNeutralCulture) && (zoomString.Length > 0) && (culture.NumberFormat != null))
{
// This will strip the percent sign (if it exists) depending on the culture information.
switch (culture.NumberFormat.PercentPositivePattern)
{
case 0: // n %
case 1: // n%
// Remove the last character if it is a percent sign
if (zoomString.Length - 1 == zoomString.LastIndexOf(
culture.NumberFormat.PercentSymbol,
StringComparison.CurrentCultureIgnoreCase))
{
zoomString = zoomString.Substring(0, zoomString.Length - 1);
}
break;
case 2: // %n
// Remove the first character if it is a percent sign.
if (0 == zoomString.IndexOf(
culture.NumberFormat.PercentSymbol,
StringComparison.CurrentCultureIgnoreCase))
{
zoomString = zoomString.Substring(1);
}
break;
}
}
// If this conversion throws then the string wasn't a valid zoom value.
zoomValue = System.Convert.ToDouble(zoomString, culture);
isValidArg = true;
}
}
// Allow empty catch statements.
#pragma warning disable 56502
// Catch only the expected parse exceptions
catch (ArgumentOutOfRangeException) { }
catch (ArgumentNullException) { }
catch (FormatException) { }
catch (OverflowException) { }
// Disallow empty catch statements.
#pragma warning restore 56502
}
// Argument wasn't a valid percent, set error value.
if (!isValidArg)
{
return DependencyProperty.UnsetValue;
}
return zoomValue;
}
// Requested type is not supported.
else
{
return DependencyProperty.UnsetValue;
}
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- SimpleHandlerBuildProvider.cs
- SiteMap.cs
- CachedCompositeFamily.cs
- SqlInfoMessageEvent.cs
- RawStylusSystemGestureInputReport.cs
- CorrelationTokenInvalidatedHandler.cs
- SerialStream.cs
- DefaultAssemblyResolver.cs
- XmlSiteMapProvider.cs
- EditorPart.cs
- GeometryDrawing.cs
- sqlmetadatafactory.cs
- RoleExceptions.cs
- MouseActionConverter.cs
- DataBinding.cs
- BaseAsyncResult.cs
- _DigestClient.cs
- RenderTargetBitmap.cs
- DurationConverter.cs
- UnsafeNativeMethodsTablet.cs
- ResourceExpressionBuilder.cs
- SiteIdentityPermission.cs
- Serializer.cs
- Table.cs
- ExpanderAutomationPeer.cs
- RuleDefinitions.cs
- SqlFlattener.cs
- StringArrayEditor.cs
- SqlUserDefinedTypeAttribute.cs
- ErrorsHelper.cs
- IdentitySection.cs
- DocumentXmlWriter.cs
- DrawingContext.cs
- TraceFilter.cs
- Stack.cs
- XamlGridLengthSerializer.cs
- Rect.cs
- ReplyChannel.cs
- StateRuntime.cs
- ApplicationHost.cs
- TypeToken.cs
- InstancePersistenceCommandException.cs
- SetterTriggerConditionValueConverter.cs
- TextElement.cs
- HeaderedItemsControl.cs
- DictionaryContent.cs
- httpserverutility.cs
- FillRuleValidation.cs
- VisualTransition.cs
- ConfigXmlWhitespace.cs
- ASCIIEncoding.cs
- XmlSchemaSimpleContentRestriction.cs
- SiteIdentityPermission.cs
- ProcessModuleCollection.cs
- SQLInt16.cs
- Table.cs
- WinEventTracker.cs
- BamlTreeNode.cs
- SecurityTokenTypes.cs
- PresentationAppDomainManager.cs
- VerificationException.cs
- CSharpCodeProvider.cs
- SimpleHandlerBuildProvider.cs
- dsa.cs
- DataColumnCollection.cs
- BasePattern.cs
- Shape.cs
- MediaCommands.cs
- DbSource.cs
- Compilation.cs
- MethodInfo.cs
- SQLUtility.cs
- ScrollBarAutomationPeer.cs
- PointAnimation.cs
- KeyGestureConverter.cs
- _TLSstream.cs
- CopyOnWriteList.cs
- WebEvents.cs
- SchemaContext.cs
- EventLogPermissionHolder.cs
- FakeModelPropertyImpl.cs
- EllipseGeometry.cs
- InfoCardRSAOAEPKeyExchangeFormatter.cs
- CommittableTransaction.cs
- SqlNodeAnnotations.cs
- EntityModelBuildProvider.cs
- XNodeSchemaApplier.cs
- TextViewElement.cs
- DocumentSequence.cs
- ClientApiGenerator.cs
- Int32CollectionConverter.cs
- CollectionView.cs
- TableLayoutCellPaintEventArgs.cs
- MetadataArtifactLoaderXmlReaderWrapper.cs
- NonParentingControl.cs
- MediaPlayer.cs
- XsltFunctions.cs
- Padding.cs
- AsnEncodedData.cs
- NotificationContext.cs