hubFS: THE place for F#

. . . are you on The Hub?
Welcome to hubFS: THE place for F# Sign in | Join | Help
in Search

An ObservableDictionary implementation and questions about constructors

Last post 09-05-2008, 8:20 by GrahameTheHornist. 0 replies.
Sort Posts: Previous Next
  •  09-05-2008, 8:20 6919

    An ObservableDictionary implementation and questions about constructors

    This morning I implemented the following class (also attached), so that I can create a WPF application that can observe the values of a Dictionary. My question is: is there a clean way to implement the other overloaded constructors to Dictionary?

    #light
    open System.Collections.Generic
    open System.Collections.ObjectModel

    type ObservableDictionary() =
    inherit Dictionary()

    let fireOnAdded, OnAdded = Event.create()
    let fireOnCleared, OnCleared = Event.create()
    let fireOnRemoved, OnRemoved = Event.create()


    override x.Add(key,value) = base.Add(key,value); fireOnAdded (key,value)

    override x.Clear() = base.Clear(); fireOnCleared ()

    override x.Remove(key) =
    match x.TryGetValue(key) with
    | true, value -> if (base.Remove key)
    then fireOnRemoved(value); true
    else false
    | _ -> base.Remove key

    member x.ObservableValues =
    let obs = ObservableCollection(x.Values)
    OnAdded.Add (fun (_,value) -> obs.Add value)
    OnRemoved.Add (fun (value) -> obs.Remove value |> ignore)
    OnCleared.Add (fun () -> obs.Clear())
    ReadOnlyObservableCollection(obs)
View as RSS news feed in XML
Powered by Community Server, by Telligent Systems