Code:
/ DotNET / DotNET / 8.0 / untmp / WIN_WINDOWS / lh_tools_devdiv_wpf / Windows / wcp / Framework / MS / Internal / Data / DynamicValueConverter.cs / 3 / DynamicValueConverter.cs
//----------------------------------------------------------------------------
//
//
// Copyright (C) Microsoft Corporation. All rights reserved.
//
//
// Description: wrapper around default converter to dynamcially pick
// and change value converters depending on changing source and target types
//
//---------------------------------------------------------------------------
using System;
using System.Globalization;
using System.Collections;
using System.ComponentModel;
using System.Reflection;
using System.Windows;
using System.Windows.Data;
using MS.Internal; // Invariant.Assert
using System.Diagnostics;
namespace MS.Internal.Data
{
// dynamically pick and switch a default value converter to convert between source and target type
internal class DynamicValueConverter : IValueConverter
{
internal DynamicValueConverter(bool targetToSourceNeeded)
{
_targetToSourceNeeded = targetToSourceNeeded;
}
internal DynamicValueConverter(bool targetToSourceNeeded, Type sourceType, Type targetType)
{
_targetToSourceNeeded = targetToSourceNeeded;
EnsureConverter(sourceType, targetType);
}
internal object Convert(object value, Type targetType)
{
return Convert(value, targetType, null, CultureInfo.InvariantCulture);
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
object result = DependencyProperty.UnsetValue; // meaning: failure to convert
if (value != null)
{
Type sourceType = value.GetType();
EnsureConverter(sourceType, targetType);
if (_converter != null)
{
result = _converter.Convert(value, targetType, parameter, culture);
}
}
else
{
if (!targetType.IsValueType)
{
result = null;
}
}
return result;
}
public object ConvertBack(object value, Type sourceType, object parameter, CultureInfo culture)
{
object result = DependencyProperty.UnsetValue; // meaning: failure to convert
if (value != null)
{
Type targetType = value.GetType();
EnsureConverter(sourceType, targetType);
if (_converter != null)
{
result = _converter.ConvertBack(value, sourceType, parameter, culture);
}
}
else
{
if (!sourceType.IsValueType)
{
result = null;
}
}
return result;
}
private void EnsureConverter(Type sourceType, Type targetType)
{
if ((_sourceType != sourceType) || (_targetType != targetType))
{
// types have changed - get a new converter
if (sourceType != null && targetType != null)
{
// DefaultValueConverter.Create() is more sophisticated to find correct type converters,
// e.g. if source/targetType is object or well-known system types.
// if there is any change in types, give that code to come up with the correct converter
if (_engine == null)
{
_engine = DataBindEngine.CurrentDataBindEngine;
}
Invariant.Assert(_engine != null);
_converter = _engine.GetDefaultValueConverter(sourceType, targetType, _targetToSourceNeeded);
}
else
{
// if either type is null, no conversion is possible.
// Don't ask GetDefaultValueConverter - it will use null as a
// hashtable key, and crash (bug 110859).
_converter = null;
}
_sourceType = sourceType;
_targetType = targetType;
}
}
private Type _sourceType;
private Type _targetType;
private IValueConverter _converter;
private bool _targetToSourceNeeded;
private DataBindEngine _engine;
}
}
// 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
- Char.cs
- NameValueFileSectionHandler.cs
- DataGridViewMethods.cs
- XmlCompatibilityReader.cs
- versioninfo.cs
- XmlStringTable.cs
- SessionStateItemCollection.cs
- GlyphInfoList.cs
- DataGridViewAutoSizeColumnsModeEventArgs.cs
- WindowsListBox.cs
- BinaryObjectReader.cs
- ApplicationServiceHelper.cs
- XmlHierarchicalEnumerable.cs
- NoResizeHandleGlyph.cs
- OrthographicCamera.cs
- TransformerConfigurationWizardBase.cs
- CommandEventArgs.cs
- FormsAuthenticationConfiguration.cs
- SmtpNetworkElement.cs
- _RequestCacheProtocol.cs
- BindingListCollectionView.cs
- CacheVirtualItemsEvent.cs
- AccessedThroughPropertyAttribute.cs
- GridViewColumnHeaderAutomationPeer.cs
- TableChangeProcessor.cs
- TreeChangeInfo.cs
- NamedPipeTransportSecurity.cs
- SchemaEntity.cs
- DataFormats.cs
- DrawTreeNodeEventArgs.cs
- CustomErrorCollection.cs
- ToolStripItemRenderEventArgs.cs
- StringConcat.cs
- RightNameExpirationInfoPair.cs
- PreloadedPackages.cs
- SHA1.cs
- BuilderInfo.cs
- VisualBrush.cs
- DataView.cs
- FontClient.cs
- RolePrincipal.cs
- validationstate.cs
- NegotiationTokenAuthenticatorState.cs
- WizardStepBase.cs
- TimeStampChecker.cs
- DbProviderFactory.cs
- ResourceReferenceExpressionConverter.cs
- OutKeywords.cs
- OleDbDataAdapter.cs
- TextTreeTextNode.cs
- QilGenerator.cs
- MultiAsyncResult.cs
- Error.cs
- Transaction.cs
- NavigatingCancelEventArgs.cs
- CompiledRegexRunner.cs
- ToolStripContentPanelRenderEventArgs.cs
- MetadataArtifactLoaderFile.cs
- ReadOnlyHierarchicalDataSource.cs
- AlternateViewCollection.cs
- ellipse.cs
- DynamicDataManager.cs
- XPathQueryGenerator.cs
- DBDataPermission.cs
- TriggerActionCollection.cs
- XmlNotation.cs
- ListViewSortEventArgs.cs
- XmlObjectSerializerWriteContextComplexJson.cs
- DynamicFilter.cs
- ToolCreatedEventArgs.cs
- SystemSounds.cs
- TypeLibConverter.cs
- ToolStripDesignerUtils.cs
- NetCodeGroup.cs
- XmlChildEnumerator.cs
- PeerEndPoint.cs
- FileIOPermission.cs
- PanelContainerDesigner.cs
- SafeWaitHandle.cs
- StatusBar.cs
- NamespaceImport.cs
- ListGeneralPage.cs
- KnownTypes.cs
- PackagingUtilities.cs
- DataConnectionHelper.cs
- OutOfMemoryException.cs
- ADMembershipUser.cs
- TextSerializer.cs
- Inflater.cs
- UpdateEventArgs.cs
- ConfigXmlElement.cs
- TypeGeneratedEventArgs.cs
- XmlCharCheckingReader.cs
- OutputCacheSection.cs
- EventHandlerList.cs
- BasicHttpSecurityMode.cs
- ReferentialConstraint.cs
- OutKeywords.cs
- FontWeight.cs
- Rect3DValueSerializer.cs