Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / wpf / src / Framework / System / Windows / Condition.cs / 1305600 / Condition.cs
using MS.Utility; using System.IO; using System.Windows.Markup; using System.ComponentModel; using System; using System.Windows.Data; using System.Diagnostics; using System.Globalization; namespace System.Windows { ////// Condition for a multiple property or data trigger /// [XamlSetMarkupExtensionAttribute("ReceiveMarkupExtension")] [XamlSetTypeConverterAttribute("ReceiveTypeConverter")] public sealed class Condition : ISupportInitialize { ////// Constructor with no property reference nor value /// public Condition() { _property = null; _binding = null; } ////// Constructor for creating a Condition /// public Condition( DependencyProperty conditionProperty, object conditionValue ) : this(conditionProperty, conditionValue, null) { // Call Forwarded } ////// Constructor for creating a Condition with the given property /// and value instead of creating an empty one and setting values later. /// ////// This constructor does parameter validation, which before doesn't /// happen until Seal() is called. We can do it here because we get /// both at the same time. /// public Condition( DependencyProperty conditionProperty, object conditionValue, string sourceName ) { if( conditionProperty == null ) { throw new ArgumentNullException("conditionProperty"); } if( !conditionProperty.IsValidValue( conditionValue ) ) { throw new ArgumentException(SR.Get(SRID.InvalidPropertyValue, conditionValue, conditionProperty.Name)); } _property = conditionProperty; Value = conditionValue; _sourceName = sourceName; } ////// Constructor for creating a Condition with the given binding declaration. /// and value. /// public Condition( BindingBase binding, object conditionValue ) { if( binding == null ) { throw new ArgumentNullException("binding"); } Binding = binding; Value = conditionValue; } ////// DepedencyProperty of the conditional /// [Ambient] [DefaultValue(null)] public DependencyProperty Property { get { return _property; } set { if (_sealed) { throw new InvalidOperationException(SR.Get(SRID.CannotChangeAfterSealed, "Condition")); } if (_binding != null) { throw new InvalidOperationException(SR.Get(SRID.ConditionCannotUseBothPropertyAndBinding)); } _property = value; } } ////// Binding of the conditional /// [DefaultValue(null)] public BindingBase Binding { get { return _binding; } set { if (_sealed) { throw new InvalidOperationException(SR.Get(SRID.CannotChangeAfterSealed, "Condition")); } if( _property != null ) { throw new InvalidOperationException(SR.Get(SRID.ConditionCannotUseBothPropertyAndBinding)); } _binding = value; } } ////// Value of the condition (equality check) /// [TypeConverter(typeof(System.Windows.Markup.SetterTriggerConditionValueConverter))] public object Value { get { return _value; } set { if (_sealed) { throw new InvalidOperationException(SR.Get(SRID.CannotChangeAfterSealed, "Condition")); } if (value is MarkupExtension) { throw new ArgumentException(SR.Get(SRID.ConditionValueOfMarkupExtensionNotSupported, value.GetType().Name)); } if( value is Expression ) { throw new ArgumentException(SR.Get(SRID.ConditionValueOfExpressionNotSupported)); } _value = value; } } ////// The x:Name of the object whose property shall /// trigger the associated setters to be applied. /// If null, then this is the object being Styled /// and not anything under its Template Tree. /// [DefaultValue(null)] public string SourceName { get { return _sourceName; } set { if( _sealed ) { throw new InvalidOperationException(SR.Get(SRID.CannotChangeAfterSealed, "Condition")); } _sourceName = value; } } ////// Seal the condition so that it can no longer be modified /// internal void Seal(ValueLookupType type) { if (_sealed) { return; } _sealed = true; // Ensure valid condition if (_property != null && _binding != null) throw new InvalidOperationException(SR.Get(SRID.ConditionCannotUseBothPropertyAndBinding)); switch (type) { case ValueLookupType.Trigger: case ValueLookupType.PropertyTriggerResource: if (_property == null) { throw new InvalidOperationException(SR.Get(SRID.NullPropertyIllegal, "Property")); } if (!_property.IsValidValue(_value)) { throw new InvalidOperationException(SR.Get(SRID.InvalidPropertyValue, _value, _property.Name)); } break; case ValueLookupType.DataTrigger: case ValueLookupType.DataTriggerResource: if (_binding == null) { throw new InvalidOperationException(SR.Get(SRID.NullPropertyIllegal, "Binding")); } break; default: throw new InvalidOperationException(SR.Get(SRID.UnexpectedValueTypeForCondition, type)); } // Freeze the condition value StyleHelper.SealIfSealable(_value); } #region ISupportInitialize Members void ISupportInitialize.BeginInit() { } void ISupportInitialize.EndInit() { // Resolve all properties here if (_unresolvedProperty != null) { try { Property = DependencyPropertyConverter.ResolveProperty(_serviceProvider, SourceName, _unresolvedProperty); } finally { _unresolvedProperty = null; } } if (_unresolvedValue != null) { try { Value = SetterTriggerConditionValueConverter.ResolveValue(_serviceProvider, Property, _cultureInfoForTypeConverter, _unresolvedValue); } finally { _unresolvedValue = null; } } _serviceProvider = null; _cultureInfoForTypeConverter = null; } #endregion public static void ReceiveMarkupExtension(object targetObject, XamlSetMarkupExtensionEventArgs eventArgs) { if (targetObject == null) { throw new ArgumentNullException("targetObject"); } if (eventArgs == null) { throw new ArgumentNullException("eventArgs"); } Condition condition = targetObject as Condition; if (condition != null && eventArgs.Member.Name == "Binding" && eventArgs.MarkupExtension is BindingBase) { condition.Binding = eventArgs.MarkupExtension as BindingBase; eventArgs.Handled = true; } } public static void ReceiveTypeConverter(object targetObject, XamlSetTypeConverterEventArgs eventArgs) { Condition condition = targetObject as Condition; if (condition == null) { throw new ArgumentNullException("targetObject"); } if (eventArgs == null) { throw new ArgumentNullException("eventArgs"); } if (eventArgs.Member.Name == "Property") { condition._unresolvedProperty = eventArgs.Value; condition._serviceProvider = eventArgs.ServiceProvider; condition._cultureInfoForTypeConverter = eventArgs.CultureInfo; eventArgs.Handled = true; } else if (eventArgs.Member.Name == "Value") { condition._unresolvedValue = eventArgs.Value; condition._serviceProvider = eventArgs.ServiceProvider; condition._cultureInfoForTypeConverter = eventArgs.CultureInfo; eventArgs.Handled = true; } } private bool _sealed = false; private DependencyProperty _property; private BindingBase _binding; private object _value = DependencyProperty.UnsetValue; private string _sourceName = null; private object _unresolvedProperty = null; private object _unresolvedValue = null; private ITypeDescriptorContext _serviceProvider = null; private CultureInfo _cultureInfoForTypeConverter = null; } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved. using MS.Utility; using System.IO; using System.Windows.Markup; using System.ComponentModel; using System; using System.Windows.Data; using System.Diagnostics; using System.Globalization; namespace System.Windows { ////// Condition for a multiple property or data trigger /// [XamlSetMarkupExtensionAttribute("ReceiveMarkupExtension")] [XamlSetTypeConverterAttribute("ReceiveTypeConverter")] public sealed class Condition : ISupportInitialize { ////// Constructor with no property reference nor value /// public Condition() { _property = null; _binding = null; } ////// Constructor for creating a Condition /// public Condition( DependencyProperty conditionProperty, object conditionValue ) : this(conditionProperty, conditionValue, null) { // Call Forwarded } ////// Constructor for creating a Condition with the given property /// and value instead of creating an empty one and setting values later. /// ////// This constructor does parameter validation, which before doesn't /// happen until Seal() is called. We can do it here because we get /// both at the same time. /// public Condition( DependencyProperty conditionProperty, object conditionValue, string sourceName ) { if( conditionProperty == null ) { throw new ArgumentNullException("conditionProperty"); } if( !conditionProperty.IsValidValue( conditionValue ) ) { throw new ArgumentException(SR.Get(SRID.InvalidPropertyValue, conditionValue, conditionProperty.Name)); } _property = conditionProperty; Value = conditionValue; _sourceName = sourceName; } ////// Constructor for creating a Condition with the given binding declaration. /// and value. /// public Condition( BindingBase binding, object conditionValue ) { if( binding == null ) { throw new ArgumentNullException("binding"); } Binding = binding; Value = conditionValue; } ////// DepedencyProperty of the conditional /// [Ambient] [DefaultValue(null)] public DependencyProperty Property { get { return _property; } set { if (_sealed) { throw new InvalidOperationException(SR.Get(SRID.CannotChangeAfterSealed, "Condition")); } if (_binding != null) { throw new InvalidOperationException(SR.Get(SRID.ConditionCannotUseBothPropertyAndBinding)); } _property = value; } } ////// Binding of the conditional /// [DefaultValue(null)] public BindingBase Binding { get { return _binding; } set { if (_sealed) { throw new InvalidOperationException(SR.Get(SRID.CannotChangeAfterSealed, "Condition")); } if( _property != null ) { throw new InvalidOperationException(SR.Get(SRID.ConditionCannotUseBothPropertyAndBinding)); } _binding = value; } } ////// Value of the condition (equality check) /// [TypeConverter(typeof(System.Windows.Markup.SetterTriggerConditionValueConverter))] public object Value { get { return _value; } set { if (_sealed) { throw new InvalidOperationException(SR.Get(SRID.CannotChangeAfterSealed, "Condition")); } if (value is MarkupExtension) { throw new ArgumentException(SR.Get(SRID.ConditionValueOfMarkupExtensionNotSupported, value.GetType().Name)); } if( value is Expression ) { throw new ArgumentException(SR.Get(SRID.ConditionValueOfExpressionNotSupported)); } _value = value; } } ////// The x:Name of the object whose property shall /// trigger the associated setters to be applied. /// If null, then this is the object being Styled /// and not anything under its Template Tree. /// [DefaultValue(null)] public string SourceName { get { return _sourceName; } set { if( _sealed ) { throw new InvalidOperationException(SR.Get(SRID.CannotChangeAfterSealed, "Condition")); } _sourceName = value; } } ////// Seal the condition so that it can no longer be modified /// internal void Seal(ValueLookupType type) { if (_sealed) { return; } _sealed = true; // Ensure valid condition if (_property != null && _binding != null) throw new InvalidOperationException(SR.Get(SRID.ConditionCannotUseBothPropertyAndBinding)); switch (type) { case ValueLookupType.Trigger: case ValueLookupType.PropertyTriggerResource: if (_property == null) { throw new InvalidOperationException(SR.Get(SRID.NullPropertyIllegal, "Property")); } if (!_property.IsValidValue(_value)) { throw new InvalidOperationException(SR.Get(SRID.InvalidPropertyValue, _value, _property.Name)); } break; case ValueLookupType.DataTrigger: case ValueLookupType.DataTriggerResource: if (_binding == null) { throw new InvalidOperationException(SR.Get(SRID.NullPropertyIllegal, "Binding")); } break; default: throw new InvalidOperationException(SR.Get(SRID.UnexpectedValueTypeForCondition, type)); } // Freeze the condition value StyleHelper.SealIfSealable(_value); } #region ISupportInitialize Members void ISupportInitialize.BeginInit() { } void ISupportInitialize.EndInit() { // Resolve all properties here if (_unresolvedProperty != null) { try { Property = DependencyPropertyConverter.ResolveProperty(_serviceProvider, SourceName, _unresolvedProperty); } finally { _unresolvedProperty = null; } } if (_unresolvedValue != null) { try { Value = SetterTriggerConditionValueConverter.ResolveValue(_serviceProvider, Property, _cultureInfoForTypeConverter, _unresolvedValue); } finally { _unresolvedValue = null; } } _serviceProvider = null; _cultureInfoForTypeConverter = null; } #endregion public static void ReceiveMarkupExtension(object targetObject, XamlSetMarkupExtensionEventArgs eventArgs) { if (targetObject == null) { throw new ArgumentNullException("targetObject"); } if (eventArgs == null) { throw new ArgumentNullException("eventArgs"); } Condition condition = targetObject as Condition; if (condition != null && eventArgs.Member.Name == "Binding" && eventArgs.MarkupExtension is BindingBase) { condition.Binding = eventArgs.MarkupExtension as BindingBase; eventArgs.Handled = true; } } public static void ReceiveTypeConverter(object targetObject, XamlSetTypeConverterEventArgs eventArgs) { Condition condition = targetObject as Condition; if (condition == null) { throw new ArgumentNullException("targetObject"); } if (eventArgs == null) { throw new ArgumentNullException("eventArgs"); } if (eventArgs.Member.Name == "Property") { condition._unresolvedProperty = eventArgs.Value; condition._serviceProvider = eventArgs.ServiceProvider; condition._cultureInfoForTypeConverter = eventArgs.CultureInfo; eventArgs.Handled = true; } else if (eventArgs.Member.Name == "Value") { condition._unresolvedValue = eventArgs.Value; condition._serviceProvider = eventArgs.ServiceProvider; condition._cultureInfoForTypeConverter = eventArgs.CultureInfo; eventArgs.Handled = true; } } private bool _sealed = false; private DependencyProperty _property; private BindingBase _binding; private object _value = DependencyProperty.UnsetValue; private string _sourceName = null; private object _unresolvedProperty = null; private object _unresolvedValue = null; private ITypeDescriptorContext _serviceProvider = null; private CultureInfo _cultureInfoForTypeConverter = null; } } // 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
- SqlConnectionString.cs
- regiisutil.cs
- _NativeSSPI.cs
- TextBox.cs
- _WebProxyDataBuilder.cs
- TiffBitmapEncoder.cs
- WaitForChangedResult.cs
- ProcessHostFactoryHelper.cs
- AsyncPostBackErrorEventArgs.cs
- IItemContainerGenerator.cs
- StrongName.cs
- WebContext.cs
- Matrix3D.cs
- SqlStatistics.cs
- _Semaphore.cs
- cookiecollection.cs
- Line.cs
- TokenizerHelper.cs
- DetectRunnableInstancesTask.cs
- MethodRental.cs
- WebContext.cs
- IODescriptionAttribute.cs
- MultiPageTextView.cs
- ImageFormatConverter.cs
- ElementInit.cs
- DataGridViewCellStateChangedEventArgs.cs
- RewritingSimplifier.cs
- RenderOptions.cs
- TemplateBuilder.cs
- ErrorStyle.cs
- DefaultAsyncDataDispatcher.cs
- TextWriter.cs
- ListViewInsertEventArgs.cs
- GridViewColumnCollectionChangedEventArgs.cs
- loginstatus.cs
- XmlElementCollection.cs
- AuthStoreRoleProvider.cs
- InternalSafeNativeMethods.cs
- ApplicationFileCodeDomTreeGenerator.cs
- TraceRecords.cs
- MetabaseServerConfig.cs
- SupportingTokenAuthenticatorSpecification.cs
- EditCommandColumn.cs
- Triangle.cs
- BamlLocalizabilityResolver.cs
- FreeFormPanel.cs
- ProbeDuplexCD1AsyncResult.cs
- FileIOPermission.cs
- PackWebRequest.cs
- Compiler.cs
- Rectangle.cs
- ControlBuilderAttribute.cs
- SchemaAttDef.cs
- assemblycache.cs
- XPathMultyIterator.cs
- DnsPermission.cs
- Utils.cs
- SecureUICommand.cs
- DataGridViewButtonCell.cs
- Region.cs
- Image.cs
- validationstate.cs
- ADConnectionHelper.cs
- ProfilePropertySettingsCollection.cs
- SmtpMail.cs
- HttpHandlerAction.cs
- ComponentEditorForm.cs
- InertiaTranslationBehavior.cs
- ExpressionVisitor.cs
- TreeNodeClickEventArgs.cs
- SchemaNamespaceManager.cs
- RequestCachePolicy.cs
- CodeGenerator.cs
- BitmapEncoder.cs
- ClientBuildManagerCallback.cs
- MenuStrip.cs
- ConfigXmlAttribute.cs
- RSAPKCS1KeyExchangeDeformatter.cs
- ChannelSinkStacks.cs
- ApplicationHost.cs
- EdmFunction.cs
- NullableLongMinMaxAggregationOperator.cs
- Events.cs
- MainMenu.cs
- XmlSchemaObjectTable.cs
- PasswordBox.cs
- ActivityFunc.cs
- CatalogPartCollection.cs
- path.cs
- ConditionalDesigner.cs
- TextSerializer.cs
- XPathNavigator.cs
- SharedPerformanceCounter.cs
- ActivityBuilderHelper.cs
- ProcessModelSection.cs
- XmlName.cs
- ProbeMatches11.cs
- DataGridTextBoxColumn.cs
- UnaryNode.cs
- InputLangChangeRequestEvent.cs