WPF/Silverlight Attached Property

Example of a WPF/Silverlight Attached Property (AP). This is a port of a C# AP implementation that can be found at http://www.silverlightshow.net/items/Attached-Properties-in-Silverlight.aspx.

Copy Source
Copy Link
Tools:
 1: open System.Windows
 2: open System.Windows.Controls
 3: 
 4: // Example ported from http://www.silverlightshow.net/items/Attached-Properties-in-Silverlight.aspx
 5: type TabPanel() = 
 6:    inherit StackPanel()  
 7:    /// Register the attached property
 8:    static member TabStopProperty = 
 9:        DependencyProperty.RegisterAttached("TabStop", typeof<bool>, typeof<TabPanel>, null) 
10:    /// Set the property
11:    static member SetTabStop (element:UIElement, value:obj) =
12:        let _, boolValue = bool.TryParse (string value)
13:        element.SetValue(TabPanel.TabStopProperty, boolValue)
14:    /// Get the property
15:    static member GetTabStop (element:UIElement) = 
16:        element.GetValue TabPanel.TabStopProperty                                            
namespace System
namespace System.Windows
namespace System.Windows.Controls
type TabPanel =
  class
    inherit StackPanel
    new : unit -> TabPanel
    static member GetTabStop : element:UIElement -> obj
    static member SetTabStop : element:UIElement * value:obj -> unit
    static member TabStopProperty : DependencyProperty
  end

Full name: Snippet.TabPanel

  type: TabPanel
  implements: Media.Composition.DUCE.IResource
  implements: Media.Animation.IAnimatable
  implements: IFrameworkInputElement
  implements: IInputElement
  implements: System.ComponentModel.ISupportInitialize
  implements: Markup.IHaveResources
  implements: Markup.IQueryAmbient
  implements: Markup.IAddChild
  implements: Primitives.IScrollInfo
  inherits: StackPanel
  inherits: Panel
  inherits: FrameworkElement
  inherits: UIElement
  inherits: Media.Visual
  inherits: DependencyObject
  inherits: Threading.DispatcherObject
type StackPanel =
  class
    inherit System.Windows.Controls.Panel
    new : unit -> System.Windows.Controls.StackPanel
    member CanHorizontallyScroll : bool with get, set
    member CanVerticallyScroll : bool with get, set
    member ExtentHeight : float
    member ExtentWidth : float
    member HorizontalOffset : float
    member LineDown : unit -> unit
    member LineLeft : unit -> unit
    member LineRight : unit -> unit
    member LineUp : unit -> unit
    member MakeVisible : System.Windows.Media.Visual * System.Windows.Rect -> System.Windows.Rect
    member MouseWheelDown : unit -> unit
    member MouseWheelLeft : unit -> unit
    member MouseWheelRight : unit -> unit
    member MouseWheelUp : unit -> unit
    member Orientation : System.Windows.Controls.Orientation with get, set
    member PageDown : unit -> unit
    member PageLeft : unit -> unit
    member PageRight : unit -> unit
    member PageUp : unit -> unit
    member ScrollOwner : System.Windows.Controls.ScrollViewer with get, set
    member SetHorizontalOffset : float -> unit
    member SetVerticalOffset : float -> unit
    member VerticalOffset : float
    member ViewportHeight : float
    member ViewportWidth : float
    static val OrientationProperty : System.Windows.DependencyProperty
  end

Full name: System.Windows.Controls.StackPanel

  type: StackPanel
  implements: Media.Composition.DUCE.IResource
  implements: Media.Animation.IAnimatable
  implements: IFrameworkInputElement
  implements: IInputElement
  implements: System.ComponentModel.ISupportInitialize
  implements: Markup.IHaveResources
  implements: Markup.IQueryAmbient
  implements: Markup.IAddChild
  implements: Primitives.IScrollInfo
  inherits: Panel
  inherits: FrameworkElement
  inherits: UIElement
  inherits: Media.Visual
  inherits: DependencyObject
  inherits: Threading.DispatcherObject
static member TabPanel.TabStopProperty : DependencyProperty

Full name: Snippet.TabPanel.TabStopProperty

Register the attached property
type DependencyProperty =
  class
    member AddOwner : System.Type -> System.Windows.DependencyProperty
    member AddOwner : System.Type * System.Windows.PropertyMetadata -> System.Windows.DependencyProperty
    member DefaultMetadata : System.Windows.PropertyMetadata
    member GetHashCode : unit -> int
    member GetMetadata : System.Type -> System.Windows.PropertyMetadata
    member GetMetadata : System.Windows.DependencyObject -> System.Windows.PropertyMetadata
    member GetMetadata : System.Windows.DependencyObjectType -> System.Windows.PropertyMetadata
    member GlobalIndex : int
    member IsValidType : obj -> bool
    member IsValidValue : obj -> bool
    member Name : string
    member OverrideMetadata : System.Type * System.Windows.PropertyMetadata -> unit
    member OverrideMetadata : System.Type * System.Windows.PropertyMetadata * System.Windows.DependencyPropertyKey -> unit
    member OwnerType : System.Type
    member PropertyType : System.Type
    member ReadOnly : bool
    member ToString : unit -> string
    member ValidateValueCallback : System.Windows.ValidateValueCallback
    static val UnsetValue : obj
    static member Register : string * System.Type * System.Type -> System.Windows.DependencyProperty
    static member Register : string * System.Type * System.Type * System.Windows.PropertyMetadata -> System.Windows.DependencyProperty
    static member Register : string * System.Type * System.Type * System.Windows.PropertyMetadata * System.Windows.ValidateValueCallback -> System.Windows.DependencyProperty
    static member RegisterAttached : string * System.Type * System.Type -> System.Windows.DependencyProperty
    static member RegisterAttached : string * System.Type * System.Type * System.Windows.PropertyMetadata -> System.Windows.DependencyProperty
    static member RegisterAttached : string * System.Type * System.Type * System.Windows.PropertyMetadata * System.Windows.ValidateValueCallback -> System.Windows.DependencyProperty
    static member RegisterAttachedReadOnly : string * System.Type * System.Type * System.Windows.PropertyMetadata -> System.Windows.DependencyPropertyKey
    static member RegisterAttachedReadOnly : string * System.Type * System.Type * System.Windows.PropertyMetadata * System.Windows.ValidateValueCallback -> System.Windows.DependencyPropertyKey
    static member RegisterReadOnly : string * System.Type * System.Type * System.Windows.PropertyMetadata -> System.Windows.DependencyPropertyKey
    static member RegisterReadOnly : string * System.Type * System.Type * System.Windows.PropertyMetadata * System.Windows.ValidateValueCallback -> System.Windows.DependencyPropertyKey
  end

Full name: System.Windows.DependencyProperty
Multiple overloads
DependencyProperty.RegisterAttached(name: string, propertyType: System.Type, ownerType: System.Type) : DependencyProperty
DependencyProperty.RegisterAttached(name: string, propertyType: System.Type, ownerType: System.Type, defaultMetadata: PropertyMetadata) : DependencyProperty
DependencyProperty.RegisterAttached(name: string, propertyType: System.Type, ownerType: System.Type, defaultMetadata: PropertyMetadata, validateValueCallback: ValidateValueCallback) : DependencyProperty
val typeof<'T> : System.Type

Full name: Microsoft.FSharp.Core.Operators.typeof
type bool = System.Boolean

Full name: Microsoft.FSharp.Core.bool

  type: bool
  implements: System.IComparable
  implements: System.IConvertible
  implements: System.IComparable<bool>
  implements: System.IEquatable<bool>
  inherits: System.ValueType
static member TabPanel.SetTabStop : element:UIElement * value:obj -> unit

Full name: Snippet.TabPanel.SetTabStop

Set the property
val element : UIElement

  type: UIElement
  implements: Media.Composition.DUCE.IResource
  implements: Media.Animation.IAnimatable
  implements: IInputElement
  inherits: Media.Visual
  inherits: DependencyObject
  inherits: Threading.DispatcherObject
type UIElement =
  class
    inherit System.Windows.Media.Visual
    new : unit -> System.Windows.UIElement
    member AddHandler : System.Windows.RoutedEvent * System.Delegate -> unit
    member AddHandler : System.Windows.RoutedEvent * System.Delegate * bool -> unit
    member AddToEventRoute : System.Windows.EventRoute * System.Windows.RoutedEventArgs -> unit
    member AllowDrop : bool with get, set
    member ApplyAnimationClock : System.Windows.DependencyProperty * System.Windows.Media.Animation.AnimationClock -> unit
    member ApplyAnimationClock : System.Windows.DependencyProperty * System.Windows.Media.Animation.AnimationClock * System.Windows.Media.Animation.HandoffBehavior -> unit
    member AreAnyTouchesCaptured : bool
    member AreAnyTouchesCapturedWithin : bool
    member AreAnyTouchesDirectlyOver : bool
    member AreAnyTouchesOver : bool
    member Arrange : System.Windows.Rect -> unit
    member BeginAnimation : System.Windows.DependencyProperty * System.Windows.Media.Animation.AnimationTimeline -> unit
    member BeginAnimation : System.Windows.DependencyProperty * System.Windows.Media.Animation.AnimationTimeline * System.Windows.Media.Animation.HandoffBehavior -> unit
    member BitmapEffect : System.Windows.Media.Effects.BitmapEffect with get, set
    member BitmapEffectInput : System.Windows.Media.Effects.BitmapEffectInput with get, set
    member CacheMode : System.Windows.Media.CacheMode with get, set
    member CaptureMouse : unit -> bool
    member CaptureStylus : unit -> bool
    member CaptureTouch : System.Windows.Input.TouchDevice -> bool
    member Clip : System.Windows.Media.Geometry with get, set
    member ClipToBounds : bool with get, set
    member CommandBindings : System.Windows.Input.CommandBindingCollection
    member DesiredSize : System.Windows.Size
    member Effect : System.Windows.Media.Effects.Effect with get, set
    member Focus : unit -> bool
    member Focusable : bool with get, set
    member GetAnimationBaseValue : System.Windows.DependencyProperty -> obj
    member HasAnimatedProperties : bool
    member InputBindings : System.Windows.Input.InputBindingCollection
    member InputHitTest : System.Windows.Point -> System.Windows.IInputElement
    member InvalidateArrange : unit -> unit
    member InvalidateMeasure : unit -> unit
    member InvalidateVisual : unit -> unit
    member IsArrangeValid : bool
    member IsEnabled : bool with get, set
    member IsFocused : bool
    member IsHitTestVisible : bool with get, set
    member IsInputMethodEnabled : bool
    member IsKeyboardFocusWithin : bool
    member IsKeyboardFocused : bool
    member IsManipulationEnabled : bool with get, set
    member IsMeasureValid : bool
    member IsMouseCaptureWithin : bool
    member IsMouseCaptured : bool
    member IsMouseDirectlyOver : bool
    member IsMouseOver : bool
    member IsStylusCaptureWithin : bool
    member IsStylusCaptured : bool
    member IsStylusDirectlyOver : bool
    member IsStylusOver : bool
    member IsVisible : bool
    member Measure : System.Windows.Size -> unit
    member MoveFocus : System.Windows.Input.TraversalRequest -> bool
    member Opacity : float with get, set
    member OpacityMask : System.Windows.Media.Brush with get, set
    member PersistId : int
    member PredictFocus : System.Windows.Input.FocusNavigationDirection -> System.Windows.DependencyObject
    member RaiseEvent : System.Windows.RoutedEventArgs -> unit
    member ReleaseAllTouchCaptures : unit -> unit
    member ReleaseMouseCapture : unit -> unit
    member ReleaseStylusCapture : unit -> unit
    member ReleaseTouchCapture : System.Windows.Input.TouchDevice -> bool
    member RemoveHandler : System.Windows.RoutedEvent * System.Delegate -> unit
    member RenderSize : System.Windows.Size with get, set
    member RenderTransform : System.Windows.Media.Transform with get, set
    member RenderTransformOrigin : System.Windows.Point with get, set
    member ShouldSerializeCommandBindings : unit -> bool
    member ShouldSerializeInputBindings : unit -> bool
    member SnapsToDevicePixels : bool with get, set
    member TouchesCaptured : System.Collections.Generic.IEnumerable<System.Windows.Input.TouchDevice>
    member TouchesCapturedWithin : System.Collections.Generic.IEnumerable<System.Windows.Input.TouchDevice>
    member TouchesDirectlyOver : System.Collections.Generic.IEnumerable<System.Windows.Input.TouchDevice>
    member TouchesOver : System.Collections.Generic.IEnumerable<System.Windows.Input.TouchDevice>
    member TranslatePoint : System.Windows.Point * System.Windows.UIElement -> System.Windows.Point
    member Uid : string with get, set
    member UpdateLayout : unit -> unit
    member Visibility : System.Windows.Visibility with get, set
    static val PreviewMouseDownEvent : System.Windows.RoutedEvent
    static val MouseDownEvent : System.Windows.RoutedEvent
    static val PreviewMouseUpEvent : System.Windows.RoutedEvent
    static val MouseUpEvent : System.Windows.RoutedEvent
    static val PreviewMouseLeftButtonDownEvent : System.Windows.RoutedEvent
    static val MouseLeftButtonDownEvent : System.Windows.RoutedEvent
    static val PreviewMouseLeftButtonUpEvent : System.Windows.RoutedEvent
    static val MouseLeftButtonUpEvent : System.Windows.RoutedEvent
    static val PreviewMouseRightButtonDownEvent : System.Windows.RoutedEvent
    static val MouseRightButtonDownEvent : System.Windows.RoutedEvent
    static val PreviewMouseRightButtonUpEvent : System.Windows.RoutedEvent
    static val MouseRightButtonUpEvent : System.Windows.RoutedEvent
    static val PreviewMouseMoveEvent : System.Windows.RoutedEvent
    static val MouseMoveEvent : System.Windows.RoutedEvent
    static val PreviewMouseWheelEvent : System.Windows.RoutedEvent
    static val MouseWheelEvent : System.Windows.RoutedEvent
    static val MouseEnterEvent : System.Windows.RoutedEvent
    static val MouseLeaveEvent : System.Windows.RoutedEvent
    static val GotMouseCaptureEvent : System.Windows.RoutedEvent
    static val LostMouseCaptureEvent : System.Windows.RoutedEvent
    static val QueryCursorEvent : System.Windows.RoutedEvent
    static val PreviewStylusDownEvent : System.Windows.RoutedEvent
    static val StylusDownEvent : System.Windows.RoutedEvent
    static val PreviewStylusUpEvent : System.Windows.RoutedEvent
    static val StylusUpEvent : System.Windows.RoutedEvent
    static val PreviewStylusMoveEvent : System.Windows.RoutedEvent
    static val StylusMoveEvent : System.Windows.RoutedEvent
    static val PreviewStylusInAirMoveEvent : System.Windows.RoutedEvent
    static val StylusInAirMoveEvent : System.Windows.RoutedEvent
    static val StylusEnterEvent : System.Windows.RoutedEvent
    static val StylusLeaveEvent : System.Windows.RoutedEvent
    static val PreviewStylusInRangeEvent : System.Windows.RoutedEvent
    static val StylusInRangeEvent : System.Windows.RoutedEvent
    static val PreviewStylusOutOfRangeEvent : System.Windows.RoutedEvent
    static val StylusOutOfRangeEvent : System.Windows.RoutedEvent
    static val PreviewStylusSystemGestureEvent : System.Windows.RoutedEvent
    static val StylusSystemGestureEvent : System.Windows.RoutedEvent
    static val GotStylusCaptureEvent : System.Windows.RoutedEvent
    static val LostStylusCaptureEvent : System.Windows.RoutedEvent
    static val StylusButtonDownEvent : System.Windows.RoutedEvent
    static val StylusButtonUpEvent : System.Windows.RoutedEvent
    static val PreviewStylusButtonDownEvent : System.Windows.RoutedEvent
    static val PreviewStylusButtonUpEvent : System.Windows.RoutedEvent
    static val PreviewKeyDownEvent : System.Windows.RoutedEvent
    static val KeyDownEvent : System.Windows.RoutedEvent
    static val PreviewKeyUpEvent : System.Windows.RoutedEvent
    static val KeyUpEvent : System.Windows.RoutedEvent
    static val PreviewGotKeyboardFocusEvent : System.Windows.RoutedEvent
    static val GotKeyboardFocusEvent : System.Windows.RoutedEvent
    static val PreviewLostKeyboardFocusEvent : System.Windows.RoutedEvent
    static val LostKeyboardFocusEvent : System.Windows.RoutedEvent
    static val PreviewTextInputEvent : System.Windows.RoutedEvent
    static val TextInputEvent : System.Windows.RoutedEvent
    static val PreviewQueryContinueDragEvent : System.Windows.RoutedEvent
    static val QueryContinueDragEvent : System.Windows.RoutedEvent
    static val PreviewGiveFeedbackEvent : System.Windows.RoutedEvent
    static val GiveFeedbackEvent : System.Windows.RoutedEvent
    static val PreviewDragEnterEvent : System.Windows.RoutedEvent
    static val DragEnterEvent : System.Windows.RoutedEvent
    static val PreviewDragOverEvent : System.Windows.RoutedEvent
    static val DragOverEvent : System.Windows.RoutedEvent
    static val PreviewDragLeaveEvent : System.Windows.RoutedEvent
    static val DragLeaveEvent : System.Windows.RoutedEvent
    static val PreviewDropEvent : System.Windows.RoutedEvent
    static val DropEvent : System.Windows.RoutedEvent
    static val PreviewTouchDownEvent : System.Windows.RoutedEvent
    static val TouchDownEvent : System.Windows.RoutedEvent
    static val PreviewTouchMoveEvent : System.Windows.RoutedEvent
    static val TouchMoveEvent : System.Windows.RoutedEvent
    static val PreviewTouchUpEvent : System.Windows.RoutedEvent
    static val TouchUpEvent : System.Windows.RoutedEvent
    static val GotTouchCaptureEvent : System.Windows.RoutedEvent
    static val LostTouchCaptureEvent : System.Windows.RoutedEvent
    static val TouchEnterEvent : System.Windows.RoutedEvent
    static val TouchLeaveEvent : System.Windows.RoutedEvent
    static val IsMouseDirectlyOverProperty : System.Windows.DependencyProperty
    static val IsMouseOverProperty : System.Windows.DependencyProperty
    static val IsStylusOverProperty : System.Windows.DependencyProperty
    static val IsKeyboardFocusWithinProperty : System.Windows.DependencyProperty
    static val IsMouseCapturedProperty : System.Windows.DependencyProperty
    static val IsMouseCaptureWithinProperty : System.Windows.DependencyProperty
    static val IsStylusDirectlyOverProperty : System.Windows.DependencyProperty
    static val IsStylusCapturedProperty : System.Windows.DependencyProperty
    static val IsStylusCaptureWithinProperty : System.Windows.DependencyProperty
    static val IsKeyboardFocusedProperty : System.Windows.DependencyProperty
    static val AreAnyTouchesDirectlyOverProperty : System.Windows.DependencyProperty
    static val AreAnyTouchesOverProperty : System.Windows.DependencyProperty
    static val AreAnyTouchesCapturedProperty : System.Windows.DependencyProperty
    static val AreAnyTouchesCapturedWithinProperty : System.Windows.DependencyProperty
    static val AllowDropProperty : System.Windows.DependencyProperty
    static val RenderTransformProperty : System.Windows.DependencyProperty
    static val RenderTransformOriginProperty : System.Windows.DependencyProperty
    static val OpacityProperty : System.Windows.DependencyProperty
    static val OpacityMaskProperty : System.Windows.DependencyProperty
    static val BitmapEffectProperty : System.Windows.DependencyProperty
    static val EffectProperty : System.Windows.DependencyProperty
    static val BitmapEffectInputProperty : System.Windows.DependencyProperty
    static val CacheModeProperty : System.Windows.DependencyProperty
    static val UidProperty : System.Windows.DependencyProperty
    static val VisibilityProperty : System.Windows.DependencyProperty
    static val ClipToBoundsProperty : System.Windows.DependencyProperty
    static val ClipProperty : System.Windows.DependencyProperty
    static val SnapsToDevicePixelsProperty : System.Windows.DependencyProperty
    static val GotFocusEvent : System.Windows.RoutedEvent
    static val LostFocusEvent : System.Windows.RoutedEvent
    static val IsFocusedProperty : System.Windows.DependencyProperty
    static val IsEnabledProperty : System.Windows.DependencyProperty
    static val IsHitTestVisibleProperty : System.Windows.DependencyProperty
    static val IsVisibleProperty : System.Windows.DependencyProperty
    static val FocusableProperty : System.Windows.DependencyProperty
    static val IsManipulationEnabledProperty : System.Windows.DependencyProperty
    static val ManipulationStartingEvent : System.Windows.RoutedEvent
    static val ManipulationStartedEvent : System.Windows.RoutedEvent
    static val ManipulationDeltaEvent : System.Windows.RoutedEvent
    static val ManipulationInertiaStartingEvent : System.Windows.RoutedEvent
    static val ManipulationBoundaryFeedbackEvent : System.Windows.RoutedEvent
    static val ManipulationCompletedEvent : System.Windows.RoutedEvent
  end

Full name: System.Windows.UIElement

  type: UIElement
  implements: Media.Composition.DUCE.IResource
  implements: Media.Animation.IAnimatable
  implements: IInputElement
  inherits: Media.Visual
  inherits: DependencyObject
  inherits: Threading.DispatcherObject
val value : obj
type obj = System.Object

Full name: Microsoft.FSharp.Core.obj
val boolValue : bool

  type: bool
  implements: System.IComparable
  implements: System.IConvertible
  implements: System.IComparable<bool>
  implements: System.IEquatable<bool>
  inherits: System.ValueType
System.Boolean.TryParse(value: string, result: byref<bool>) : bool
Multiple items
val string : 'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------

type string = System.String

Full name: Microsoft.FSharp.Core.string

  type: string
  implements: System.IComparable
  implements: System.ICloneable
  implements: System.IConvertible
  implements: System.IComparable<string>
  implements: seq<char>
  implements: System.Collections.IEnumerable
  implements: System.IEquatable<string>
Multiple overloads
DependencyObject.SetValue(key: DependencyPropertyKey, value: obj) : unit
DependencyObject.SetValue(dp: DependencyProperty, value: obj) : unit
property TabPanel.TabStopProperty: DependencyProperty

Register the attached property
static member TabPanel.GetTabStop : element:UIElement -> obj

Full name: Snippet.TabPanel.GetTabStop

Get the property
DependencyObject.GetValue(dp: DependencyProperty) : obj

More information

Link: http://fssnip.net/69
Posted: 7 months ago
Author: Daniel Mohl (website)
Tags: WPF, Silverlight