Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / wpf / src / Framework / System / Windows / Setter.cs / 1305600 / Setter.cs
/****************************************************************************\
*
* File: Setter.cs
*
* TargetType property setting class.
*
* Copyright (C) 2004 by Microsoft Corporation. All rights reserved.
*
\***************************************************************************/
using System;
using System.ComponentModel;
using System.Windows.Markup;
using System.Windows.Data;
using System.Globalization;
#pragma warning disable 1634, 1691 // suppressing PreSharp warnings
namespace System.Windows
{
///
/// TargetType property setting class.
///
[XamlSetMarkupExtensionAttribute("ReceiveMarkupExtension")]
[XamlSetTypeConverterAttribute("ReceiveTypeConverter")]
public class Setter : SetterBase, ISupportInitialize
{
///
/// Property Setter construction - set everything to null or DependencyProperty.UnsetValue
///
public Setter()
{
}
///
/// Property Setter construction - given property and value
///
public Setter( DependencyProperty property, object value )
{
Initialize( property, value, null );
}
///
/// Property Setter construction - given property, value, and string identifier for child node.
///
public Setter( DependencyProperty property, object value, string targetName )
{
Initialize( property, value, targetName );
}
///
/// Method that does all the initialization work for the constructors.
///
private void Initialize( DependencyProperty property, object value, string target )
{
if( value == DependencyProperty.UnsetValue )
{
throw new ArgumentException(SR.Get(SRID.SetterValueCannotBeUnset));
}
CheckValidProperty(property);
// No null check for target since null is a valid value.
_property = property;
_value = value;
_target = target;
}
private void CheckValidProperty( DependencyProperty property)
{
if (property == null)
{
throw new ArgumentNullException("property");
}
if (property.ReadOnly)
{
// Read-only properties will not be consulting Style/Template/Trigger Setter for value.
// Rather than silently do nothing, throw error.
throw new ArgumentException(SR.Get(SRID.ReadOnlyPropertyNotAllowed, property.Name, GetType().Name));
}
if( property == FrameworkElement.NameProperty)
{
throw new InvalidOperationException(SR.Get(SRID.CannotHavePropertyInStyle, FrameworkElement.NameProperty.Name));
}
}
///
/// Seals this setter
///
internal override void Seal()
{
// Do the validation that can't be done until we know all of the property
// values.
DependencyProperty dp = Property;
object value = ValueInternal;
if (dp == null)
{
throw new ArgumentException(SR.Get(SRID.NullPropertyIllegal, "Setter.Property"));
}
if( String.IsNullOrEmpty(TargetName))
{
// Setter on container is not allowed to affect the StyleProperty.
if (dp == FrameworkElement.StyleProperty)
{
throw new ArgumentException(SR.Get(SRID.StylePropertyInStyleNotAllowed));
}
}
// Value needs to be valid for the DP, or a deferred reference, or one of the supported
// markup extensions.
if (!dp.IsValidValue(value))
{
// The only markup extensions supported by styles is resources and bindings.
if (value is MarkupExtension)
{
if ( !(value is DynamicResourceExtension) && !(value is System.Windows.Data.BindingBase) )
{
throw new ArgumentException(SR.Get(SRID.SetterValueOfMarkupExtensionNotSupported,
value.GetType().Name));
}
}
else if (!(value is DeferredReference))
{
throw new ArgumentException(SR.Get(SRID.InvalidSetterValue, value, dp.OwnerType, dp.Name));
}
}
// Freeze the value for the setter
StyleHelper.SealIfSealable(_value);
base.Seal();
}
///
/// Property that is being set by this setter
///
[Ambient]
[DefaultValue(null)]
[Localizability(LocalizationCategory.None, Modifiability = Modifiability.Unmodifiable, Readability = Readability.Unreadable)] // Not localizable by-default
public DependencyProperty Property
{
get { return _property; }
set
{
CheckValidProperty(value);
CheckSealed();
_property = value;
}
}
///
/// Property value that is being set by this setter
///
[System.Windows.Markup.DependsOn("Property")]
[System.Windows.Markup.DependsOn("TargetName")]
[Localizability(LocalizationCategory.None, Readability = Readability.Unreadable)] // Not localizable by-default
[TypeConverter(typeof(System.Windows.Markup.SetterTriggerConditionValueConverter))]
public object Value
{
get
{
// Inflate the deferred reference if the _value is one of those.
DeferredReference deferredReference = _value as DeferredReference;
if (deferredReference != null)
{
_value = deferredReference.GetValue(BaseValueSourceInternal.Unknown);
}
return _value;
}
set
{
if( value == DependencyProperty.UnsetValue )
{
throw new ArgumentException(SR.Get(SRID.SetterValueCannotBeUnset));
}
CheckSealed();
// No Expression support
if( value is Expression )
{
throw new ArgumentException(SR.Get(SRID.StyleValueOfExpressionNotSupported));
}
_value = value;
}
}
///
/// Internal property used so that we obtain the value as
/// is without having to inflate the DeferredReference.
///
internal object ValueInternal
{
get { return _value; }
}
///
/// When the set is directed at a child node, this string
/// identifies the intended target child node.
///
[DefaultValue(null)]
[Ambient]
public string TargetName
{
get
{
return _target;
}
set
{
// Setting to null is allowed, to clear out value.
CheckSealed();
_target = value;
}
}
public static void ReceiveMarkupExtension(object targetObject, XamlSetMarkupExtensionEventArgs eventArgs)
{
if (targetObject == null)
{
throw new ArgumentNullException("targetObject");
}
if (eventArgs == null)
{
throw new ArgumentNullException("eventArgs");
}
Setter setter = targetObject as Setter;
if (setter == null || eventArgs.Member.Name != "Value")
{
return;
}
MarkupExtension me = eventArgs.MarkupExtension;
if (me is StaticResourceExtension)
{
var sr = me as StaticResourceExtension;
setter.Value = sr.ProvideValueInternal(eventArgs.ServiceProvider, true /*allowDeferedReference*/);
eventArgs.Handled = true;
}
else if (me is DynamicResourceExtension || me is BindingBase)
{
setter.Value = me;
eventArgs.Handled = true;
}
}
public static void ReceiveTypeConverter(object targetObject, XamlSetTypeConverterEventArgs eventArgs)
{
Setter setter = targetObject as Setter;
if (setter == null)
{
throw new ArgumentNullException("targetObject");
}
if (eventArgs == null)
{
throw new ArgumentNullException("eventArgs");
}
if (eventArgs.Member.Name == "Property")
{
setter._unresolvedProperty = eventArgs.Value;
setter._serviceProvider = eventArgs.ServiceProvider;
setter._cultureInfoForTypeConverter = eventArgs.CultureInfo;
eventArgs.Handled = true;
}
else if (eventArgs.Member.Name == "Value")
{
setter._unresolvedValue = eventArgs.Value;
setter._serviceProvider = eventArgs.ServiceProvider;
setter._cultureInfoForTypeConverter = eventArgs.CultureInfo;
eventArgs.Handled = true;
}
}
#region ISupportInitialize Members
void ISupportInitialize.BeginInit()
{
}
void ISupportInitialize.EndInit()
{
// Resolve all properties here
if (_unresolvedProperty != null)
{
try
{
Property = DependencyPropertyConverter.ResolveProperty(_serviceProvider,
TargetName, _unresolvedProperty);
}
finally
{
_unresolvedProperty = null;
}
}
if (_unresolvedValue != null)
{
try
{
Value = SetterTriggerConditionValueConverter.ResolveValue(_serviceProvider,
Property, _cultureInfoForTypeConverter, _unresolvedValue);
}
finally
{
_unresolvedValue = null;
}
}
_serviceProvider = null;
_cultureInfoForTypeConverter = null;
}
#endregion
private DependencyProperty _property = null;
private object _value = DependencyProperty.UnsetValue;
private string _target = 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.
/****************************************************************************\
*
* File: Setter.cs
*
* TargetType property setting class.
*
* Copyright (C) 2004 by Microsoft Corporation. All rights reserved.
*
\***************************************************************************/
using System;
using System.ComponentModel;
using System.Windows.Markup;
using System.Windows.Data;
using System.Globalization;
#pragma warning disable 1634, 1691 // suppressing PreSharp warnings
namespace System.Windows
{
///
/// TargetType property setting class.
///
[XamlSetMarkupExtensionAttribute("ReceiveMarkupExtension")]
[XamlSetTypeConverterAttribute("ReceiveTypeConverter")]
public class Setter : SetterBase, ISupportInitialize
{
///
/// Property Setter construction - set everything to null or DependencyProperty.UnsetValue
///
public Setter()
{
}
///
/// Property Setter construction - given property and value
///
public Setter( DependencyProperty property, object value )
{
Initialize( property, value, null );
}
///
/// Property Setter construction - given property, value, and string identifier for child node.
///
public Setter( DependencyProperty property, object value, string targetName )
{
Initialize( property, value, targetName );
}
///
/// Method that does all the initialization work for the constructors.
///
private void Initialize( DependencyProperty property, object value, string target )
{
if( value == DependencyProperty.UnsetValue )
{
throw new ArgumentException(SR.Get(SRID.SetterValueCannotBeUnset));
}
CheckValidProperty(property);
// No null check for target since null is a valid value.
_property = property;
_value = value;
_target = target;
}
private void CheckValidProperty( DependencyProperty property)
{
if (property == null)
{
throw new ArgumentNullException("property");
}
if (property.ReadOnly)
{
// Read-only properties will not be consulting Style/Template/Trigger Setter for value.
// Rather than silently do nothing, throw error.
throw new ArgumentException(SR.Get(SRID.ReadOnlyPropertyNotAllowed, property.Name, GetType().Name));
}
if( property == FrameworkElement.NameProperty)
{
throw new InvalidOperationException(SR.Get(SRID.CannotHavePropertyInStyle, FrameworkElement.NameProperty.Name));
}
}
///
/// Seals this setter
///
internal override void Seal()
{
// Do the validation that can't be done until we know all of the property
// values.
DependencyProperty dp = Property;
object value = ValueInternal;
if (dp == null)
{
throw new ArgumentException(SR.Get(SRID.NullPropertyIllegal, "Setter.Property"));
}
if( String.IsNullOrEmpty(TargetName))
{
// Setter on container is not allowed to affect the StyleProperty.
if (dp == FrameworkElement.StyleProperty)
{
throw new ArgumentException(SR.Get(SRID.StylePropertyInStyleNotAllowed));
}
}
// Value needs to be valid for the DP, or a deferred reference, or one of the supported
// markup extensions.
if (!dp.IsValidValue(value))
{
// The only markup extensions supported by styles is resources and bindings.
if (value is MarkupExtension)
{
if ( !(value is DynamicResourceExtension) && !(value is System.Windows.Data.BindingBase) )
{
throw new ArgumentException(SR.Get(SRID.SetterValueOfMarkupExtensionNotSupported,
value.GetType().Name));
}
}
else if (!(value is DeferredReference))
{
throw new ArgumentException(SR.Get(SRID.InvalidSetterValue, value, dp.OwnerType, dp.Name));
}
}
// Freeze the value for the setter
StyleHelper.SealIfSealable(_value);
base.Seal();
}
///
/// Property that is being set by this setter
///
[Ambient]
[DefaultValue(null)]
[Localizability(LocalizationCategory.None, Modifiability = Modifiability.Unmodifiable, Readability = Readability.Unreadable)] // Not localizable by-default
public DependencyProperty Property
{
get { return _property; }
set
{
CheckValidProperty(value);
CheckSealed();
_property = value;
}
}
///
/// Property value that is being set by this setter
///
[System.Windows.Markup.DependsOn("Property")]
[System.Windows.Markup.DependsOn("TargetName")]
[Localizability(LocalizationCategory.None, Readability = Readability.Unreadable)] // Not localizable by-default
[TypeConverter(typeof(System.Windows.Markup.SetterTriggerConditionValueConverter))]
public object Value
{
get
{
// Inflate the deferred reference if the _value is one of those.
DeferredReference deferredReference = _value as DeferredReference;
if (deferredReference != null)
{
_value = deferredReference.GetValue(BaseValueSourceInternal.Unknown);
}
return _value;
}
set
{
if( value == DependencyProperty.UnsetValue )
{
throw new ArgumentException(SR.Get(SRID.SetterValueCannotBeUnset));
}
CheckSealed();
// No Expression support
if( value is Expression )
{
throw new ArgumentException(SR.Get(SRID.StyleValueOfExpressionNotSupported));
}
_value = value;
}
}
///
/// Internal property used so that we obtain the value as
/// is without having to inflate the DeferredReference.
///
internal object ValueInternal
{
get { return _value; }
}
///
/// When the set is directed at a child node, this string
/// identifies the intended target child node.
///
[DefaultValue(null)]
[Ambient]
public string TargetName
{
get
{
return _target;
}
set
{
// Setting to null is allowed, to clear out value.
CheckSealed();
_target = value;
}
}
public static void ReceiveMarkupExtension(object targetObject, XamlSetMarkupExtensionEventArgs eventArgs)
{
if (targetObject == null)
{
throw new ArgumentNullException("targetObject");
}
if (eventArgs == null)
{
throw new ArgumentNullException("eventArgs");
}
Setter setter = targetObject as Setter;
if (setter == null || eventArgs.Member.Name != "Value")
{
return;
}
MarkupExtension me = eventArgs.MarkupExtension;
if (me is StaticResourceExtension)
{
var sr = me as StaticResourceExtension;
setter.Value = sr.ProvideValueInternal(eventArgs.ServiceProvider, true /*allowDeferedReference*/);
eventArgs.Handled = true;
}
else if (me is DynamicResourceExtension || me is BindingBase)
{
setter.Value = me;
eventArgs.Handled = true;
}
}
public static void ReceiveTypeConverter(object targetObject, XamlSetTypeConverterEventArgs eventArgs)
{
Setter setter = targetObject as Setter;
if (setter == null)
{
throw new ArgumentNullException("targetObject");
}
if (eventArgs == null)
{
throw new ArgumentNullException("eventArgs");
}
if (eventArgs.Member.Name == "Property")
{
setter._unresolvedProperty = eventArgs.Value;
setter._serviceProvider = eventArgs.ServiceProvider;
setter._cultureInfoForTypeConverter = eventArgs.CultureInfo;
eventArgs.Handled = true;
}
else if (eventArgs.Member.Name == "Value")
{
setter._unresolvedValue = eventArgs.Value;
setter._serviceProvider = eventArgs.ServiceProvider;
setter._cultureInfoForTypeConverter = eventArgs.CultureInfo;
eventArgs.Handled = true;
}
}
#region ISupportInitialize Members
void ISupportInitialize.BeginInit()
{
}
void ISupportInitialize.EndInit()
{
// Resolve all properties here
if (_unresolvedProperty != null)
{
try
{
Property = DependencyPropertyConverter.ResolveProperty(_serviceProvider,
TargetName, _unresolvedProperty);
}
finally
{
_unresolvedProperty = null;
}
}
if (_unresolvedValue != null)
{
try
{
Value = SetterTriggerConditionValueConverter.ResolveValue(_serviceProvider,
Property, _cultureInfoForTypeConverter, _unresolvedValue);
}
finally
{
_unresolvedValue = null;
}
}
_serviceProvider = null;
_cultureInfoForTypeConverter = null;
}
#endregion
private DependencyProperty _property = null;
private object _value = DependencyProperty.UnsetValue;
private string _target = 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
- XPathExpr.cs
- XmlSerializerNamespaces.cs
- DisposableCollectionWrapper.cs
- DesignBinding.cs
- SmtpAuthenticationManager.cs
- ErrorsHelper.cs
- ShaperBuffers.cs
- TableHeaderCell.cs
- externdll.cs
- MinimizableAttributeTypeConverter.cs
- XmlNamespaceManager.cs
- SchemaComplexType.cs
- PcmConverter.cs
- Constraint.cs
- ConnectionInterfaceCollection.cs
- Group.cs
- UnknownBitmapDecoder.cs
- BitmapSourceSafeMILHandle.cs
- UpdateCommand.cs
- ScriptingProfileServiceSection.cs
- PagedControl.cs
- WebPartDescriptionCollection.cs
- AttributeAction.cs
- FormViewUpdateEventArgs.cs
- ScrollItemProviderWrapper.cs
- HttpCookiesSection.cs
- Wildcard.cs
- WindowsStatusBar.cs
- InitializationEventAttribute.cs
- GroupBox.cs
- PropertyIDSet.cs
- SelectionManager.cs
- DependsOnAttribute.cs
- SqlMetaData.cs
- XPathMultyIterator.cs
- CurrentChangingEventArgs.cs
- TextPointer.cs
- UnauthorizedAccessException.cs
- SrgsNameValueTag.cs
- ImageListStreamer.cs
- ObjectDataSourceFilteringEventArgs.cs
- SourceFileInfo.cs
- HtmlSelect.cs
- HuffCodec.cs
- XPathNodeList.cs
- ConfigXmlCDataSection.cs
- DiscoveryDocument.cs
- CryptoStream.cs
- XmlSchemaCollection.cs
- HWStack.cs
- HttpListenerResponse.cs
- DetailsViewUpdatedEventArgs.cs
- AutomationPeer.cs
- TabControl.cs
- ParameterBuilder.cs
- SerializationFieldInfo.cs
- ColumnResizeAdorner.cs
- Clipboard.cs
- ListChunk.cs
- XmlSerializationGeneratedCode.cs
- BackgroundFormatInfo.cs
- HitTestWithPointDrawingContextWalker.cs
- BinaryConverter.cs
- ellipse.cs
- Panel.cs
- ServiceModelActivity.cs
- TransformCryptoHandle.cs
- QueryOutputWriter.cs
- EntityDataSourceContainerNameConverter.cs
- StylusTip.cs
- StrongNameIdentityPermission.cs
- MailAddress.cs
- ToolStripSplitButton.cs
- SettingsPropertyNotFoundException.cs
- BuildProvider.cs
- WindowsEditBox.cs
- DataSourceProvider.cs
- BamlTreeUpdater.cs
- ContentElement.cs
- XPathNodeInfoAtom.cs
- SqlFacetAttribute.cs
- EncoderParameter.cs
- DropTarget.cs
- ProvidersHelper.cs
- PeerIPHelper.cs
- StaticContext.cs
- TimeSpanConverter.cs
- WebEventTraceProvider.cs
- UdpAnnouncementEndpoint.cs
- WindowsSolidBrush.cs
- XmlReaderSettings.cs
- MatrixAnimationUsingPath.cs
- QueryAccessibilityHelpEvent.cs
- AnnotationComponentChooser.cs
- GetParentChain.cs
- GridViewUpdatedEventArgs.cs
- TypeSource.cs
- XmlSchemaNotation.cs
- FragmentNavigationEventArgs.cs
- DesignTimeSiteMapProvider.cs