Code:
/ Net / Net / 3.5.50727.3053 / DEVDIV / depot / DevDiv / releases / whidbey / netfxsp / ndp / fx / src / WinForms / Managed / System / WinForms / ListBindingConverter.cs / 1 / ListBindingConverter.cs
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
namespace System.Windows.Forms {
using System;
using System.Diagnostics;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Collections;
using System.Globalization;
using System.Reflection;
///
///
/// [To be supplied.]
///
public class ListBindingConverter : TypeConverter {
private static Type[] ctorTypes = null; // the list of type of our ctor parameters.
private static string[] ctorParamProps = null; // the name of each property to check to see if we need to init with a ctor.
///
/// Creates our array of types on demand.
///
private static Type[] ConstructorParamaterTypes {
get {
if (ctorTypes == null) {
ctorTypes = new Type[]{typeof(string), typeof(object), typeof(string), typeof(bool), typeof(DataSourceUpdateMode), typeof(object), typeof(string), typeof(IFormatProvider)};
}
return ctorTypes;
}
}
///
/// Creates our array of param names on demand.
///
private static string[] ConstructorParameterProperties {
get {
if (ctorParamProps == null) {
ctorParamProps = new string[]{null, null, null, "FormattingEnabled", "DataSourceUpdateMode", "NullValue", "FormatString", "FormatInfo", };
}
return ctorParamProps;
}
}
///
///
/// Gets a value indicating whether this converter can
/// convert an object to the given destination type using the context.
///
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) {
if (destinationType == typeof(InstanceDescriptor)) {
return true;
}
return base.CanConvertTo(context, destinationType);
}
///
///
/// Converts the given object to another type. The most common types to convert
/// are to and from a string object. The default implementation will make a call
/// to ToString on the object if the object is valid and if the destination
/// type is string. If this cannot convert to the desitnation type, this will
/// throw a NotSupportedException.
///
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) {
if (destinationType == null) {
throw new ArgumentNullException("destinationType");
}
if (destinationType == typeof(InstanceDescriptor) && value is Binding) {
Binding b = (Binding)value;
return GetInstanceDescriptorFromValues(b);
}
return base.ConvertTo(context, culture, value, destinationType);
}
///
///
/// Creates an instance of this type given a set of property values
/// for the object. This is useful for objects that are immutable, but still
/// want to provide changable properties.
///
public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues) {
try
{
return new Binding((string)propertyValues["PropertyName"],
propertyValues["DataSource"],
(string)propertyValues["DataMember"]);
}
catch (InvalidCastException invalidCast)
{
throw new ArgumentException(SR.GetString(SR.PropertyValueInvalidEntry), invalidCast);
}
catch (NullReferenceException nullRef)
{
throw new ArgumentException(SR.GetString(SR.PropertyValueInvalidEntry), nullRef);
}
}
///
///
/// Determines if changing a value on this object should require a call to
/// CreateInstance to create a new value.
///
public override bool GetCreateInstanceSupported(ITypeDescriptorContext context) {
return true;
}
///
/// Gets the best matching ctor for a given binding and fills it out, based on the
/// state of the Binding and the optimal ctor.
///
private InstanceDescriptor GetInstanceDescriptorFromValues(Binding b) {
// The BindingFormattingDialog turns on Binding::FormattingEnabled property.
// however, when the user data binds a property using the PropertyBrowser, Binding::FormattingEnabled is set to false
// The Binding class is not a component class, so we don't have the ComponentInitialize method where we can set FormattingEnabled to true
// so we set it here. VsWhidbey 241361
b.FormattingEnabled = true;
bool isComplete = true;
int lastItem = ConstructorParameterProperties.Length - 1;
for (; lastItem >= 0; lastItem--) {
// null means no prop is available, we quit here.
//
if (ConstructorParameterProperties[lastItem] == null) {
break;
}
// get the property and see if it needs to be serialized.
//
PropertyDescriptor prop = TypeDescriptor.GetProperties(b)[ConstructorParameterProperties[lastItem]];
if (prop != null && prop.ShouldSerializeValue(b)) {
break;
}
}
// now copy the type array up to the point we quit.
//
Type[] ctorParams = new Type[lastItem + 1];
Array.Copy(ConstructorParamaterTypes, 0, ctorParams, 0, ctorParams.Length);
// Get the ctor info.
//
ConstructorInfo ctor = typeof(Binding).GetConstructor(ctorParams);
Debug.Assert(ctor != null, "Failed to find Binding ctor for types!");
if (ctor == null) {
isComplete = false;
ctor = typeof(Binding).GetConstructor(new Type[] {
typeof(string),
typeof(object),
typeof(string)});
}
// now fill in the values.
//
object[] values = new object[ctorParams.Length];
for (int i = 0; i < values.Length; i++) {
object val = null;
switch (i) {
case 0:
val = b.PropertyName;
break;
case 1:
val = b.BindToObject.DataSource;
break;
case 2:
val = b.BindToObject.BindingMemberInfo.BindingMember;
break;
default:
val = TypeDescriptor.GetProperties(b)[ConstructorParameterProperties[i]].GetValue(b);
break;
}
values[i] = val;
}
return new InstanceDescriptor(ctor, values, isComplete);
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
namespace System.Windows.Forms {
using System;
using System.Diagnostics;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Collections;
using System.Globalization;
using System.Reflection;
///
///
/// [To be supplied.]
///
public class ListBindingConverter : TypeConverter {
private static Type[] ctorTypes = null; // the list of type of our ctor parameters.
private static string[] ctorParamProps = null; // the name of each property to check to see if we need to init with a ctor.
///
/// Creates our array of types on demand.
///
private static Type[] ConstructorParamaterTypes {
get {
if (ctorTypes == null) {
ctorTypes = new Type[]{typeof(string), typeof(object), typeof(string), typeof(bool), typeof(DataSourceUpdateMode), typeof(object), typeof(string), typeof(IFormatProvider)};
}
return ctorTypes;
}
}
///
/// Creates our array of param names on demand.
///
private static string[] ConstructorParameterProperties {
get {
if (ctorParamProps == null) {
ctorParamProps = new string[]{null, null, null, "FormattingEnabled", "DataSourceUpdateMode", "NullValue", "FormatString", "FormatInfo", };
}
return ctorParamProps;
}
}
///
///
/// Gets a value indicating whether this converter can
/// convert an object to the given destination type using the context.
///
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) {
if (destinationType == typeof(InstanceDescriptor)) {
return true;
}
return base.CanConvertTo(context, destinationType);
}
///
///
/// Converts the given object to another type. The most common types to convert
/// are to and from a string object. The default implementation will make a call
/// to ToString on the object if the object is valid and if the destination
/// type is string. If this cannot convert to the desitnation type, this will
/// throw a NotSupportedException.
///
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) {
if (destinationType == null) {
throw new ArgumentNullException("destinationType");
}
if (destinationType == typeof(InstanceDescriptor) && value is Binding) {
Binding b = (Binding)value;
return GetInstanceDescriptorFromValues(b);
}
return base.ConvertTo(context, culture, value, destinationType);
}
///
///
/// Creates an instance of this type given a set of property values
/// for the object. This is useful for objects that are immutable, but still
/// want to provide changable properties.
///
public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues) {
try
{
return new Binding((string)propertyValues["PropertyName"],
propertyValues["DataSource"],
(string)propertyValues["DataMember"]);
}
catch (InvalidCastException invalidCast)
{
throw new ArgumentException(SR.GetString(SR.PropertyValueInvalidEntry), invalidCast);
}
catch (NullReferenceException nullRef)
{
throw new ArgumentException(SR.GetString(SR.PropertyValueInvalidEntry), nullRef);
}
}
///
///
/// Determines if changing a value on this object should require a call to
/// CreateInstance to create a new value.
///
public override bool GetCreateInstanceSupported(ITypeDescriptorContext context) {
return true;
}
///
/// Gets the best matching ctor for a given binding and fills it out, based on the
/// state of the Binding and the optimal ctor.
///
private InstanceDescriptor GetInstanceDescriptorFromValues(Binding b) {
// The BindingFormattingDialog turns on Binding::FormattingEnabled property.
// however, when the user data binds a property using the PropertyBrowser, Binding::FormattingEnabled is set to false
// The Binding class is not a component class, so we don't have the ComponentInitialize method where we can set FormattingEnabled to true
// so we set it here. VsWhidbey 241361
b.FormattingEnabled = true;
bool isComplete = true;
int lastItem = ConstructorParameterProperties.Length - 1;
for (; lastItem >= 0; lastItem--) {
// null means no prop is available, we quit here.
//
if (ConstructorParameterProperties[lastItem] == null) {
break;
}
// get the property and see if it needs to be serialized.
//
PropertyDescriptor prop = TypeDescriptor.GetProperties(b)[ConstructorParameterProperties[lastItem]];
if (prop != null && prop.ShouldSerializeValue(b)) {
break;
}
}
// now copy the type array up to the point we quit.
//
Type[] ctorParams = new Type[lastItem + 1];
Array.Copy(ConstructorParamaterTypes, 0, ctorParams, 0, ctorParams.Length);
// Get the ctor info.
//
ConstructorInfo ctor = typeof(Binding).GetConstructor(ctorParams);
Debug.Assert(ctor != null, "Failed to find Binding ctor for types!");
if (ctor == null) {
isComplete = false;
ctor = typeof(Binding).GetConstructor(new Type[] {
typeof(string),
typeof(object),
typeof(string)});
}
// now fill in the values.
//
object[] values = new object[ctorParams.Length];
for (int i = 0; i < values.Length; i++) {
object val = null;
switch (i) {
case 0:
val = b.PropertyName;
break;
case 1:
val = b.BindToObject.DataSource;
break;
case 2:
val = b.BindToObject.BindingMemberInfo.BindingMember;
break;
default:
val = TypeDescriptor.GetProperties(b)[ConstructorParameterProperties[i]].GetValue(b);
break;
}
values[i] = val;
}
return new InstanceDescriptor(ctor, values, isComplete);
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- UnsafeNativeMethodsMilCoreApi.cs
- DataGridViewRowCollection.cs
- DataMisalignedException.cs
- Help.cs
- SrgsElement.cs
- MeasureData.cs
- FunctionMappingTranslator.cs
- TextPatternIdentifiers.cs
- DefaultEventAttribute.cs
- PageRequestManager.cs
- Point4D.cs
- DataGridViewHitTestInfo.cs
- Errors.cs
- SpinLock.cs
- AssemblyAttributes.cs
- DiscardableAttribute.cs
- SpanIndex.cs
- FormView.cs
- MultipleViewPattern.cs
- DesignTimeTemplateParser.cs
- MatrixConverter.cs
- SplitContainer.cs
- CmsInterop.cs
- TextRenderer.cs
- ElementHost.cs
- ChannelTokenTypeConverter.cs
- MessageEncodingBindingElementImporter.cs
- GridViewHeaderRowPresenterAutomationPeer.cs
- CaseCqlBlock.cs
- Stream.cs
- SqlTypesSchemaImporter.cs
- wmiprovider.cs
- CaseInsensitiveComparer.cs
- SqlClientFactory.cs
- Model3D.cs
- TraceContextRecord.cs
- CapabilitiesUse.cs
- CuspData.cs
- RuntimeArgument.cs
- SocketPermission.cs
- PropertyKey.cs
- MobileListItemCollection.cs
- ChangePassword.cs
- GatewayDefinition.cs
- EntityViewGenerationConstants.cs
- ToolTip.cs
- StartUpEventArgs.cs
- Converter.cs
- ResourceContainer.cs
- SvcMapFileSerializer.cs
- TableCellAutomationPeer.cs
- RelationshipConstraintValidator.cs
- ColorConvertedBitmap.cs
- SystemIPGlobalProperties.cs
- PersonalizationState.cs
- ListSurrogate.cs
- ByeMessageApril2005.cs
- ToolStripManager.cs
- FormParameter.cs
- HeaderCollection.cs
- UserValidatedEventArgs.cs
- DecimalConstantAttribute.cs
- WebResponse.cs
- UpdatePanelTriggerCollection.cs
- XmlName.cs
- DocumentSequenceHighlightLayer.cs
- UserInitiatedNavigationPermission.cs
- DriveInfo.cs
- StreamHelper.cs
- SBCSCodePageEncoding.cs
- AccessorTable.cs
- PropertyItem.cs
- Transform3DGroup.cs
- SizeChangedInfo.cs
- DebugView.cs
- PathFigureCollectionValueSerializer.cs
- HoistedLocals.cs
- CharacterBuffer.cs
- XmlDataProvider.cs
- TextEditorMouse.cs
- RecipientIdentity.cs
- ColorInterpolationModeValidation.cs
- LinkUtilities.cs
- HttpApplicationFactory.cs
- ErrorWebPart.cs
- ToolboxDataAttribute.cs
- NullableFloatMinMaxAggregationOperator.cs
- RegularExpressionValidator.cs
- VBCodeProvider.cs
- TreeViewEvent.cs
- UniqueConstraint.cs
- Trigger.cs
- SqlReferenceCollection.cs
- ParameterCollectionEditor.cs
- Trace.cs
- PropertyDescriptorCollection.cs
- DataSourceSelectArguments.cs
- LogLogRecordEnumerator.cs
- ChannelTraceRecord.cs
- IdnElement.cs