Skip to content

Instantly share code, notes, and snippets.

@B-Reif
Created January 25, 2021 01:13
Show Gist options
  • Save B-Reif/1b62c0300ec2e81cb8a3e7467bd57a5c to your computer and use it in GitHub Desktop.
Save B-Reif/1b62c0300ec2e81cb8a3e7467bd57a5c to your computer and use it in GitHub Desktop.
controlled expander sketch
[<Struct>]
type ControlledState =
| Writing
| Resetting
| Listening
type ControlledExpander() =
inherit Expander()
interface IStyleable with
member this.StyleKey = typeof<Expander>
member val ControlledState : ControlledState = Listening with get, set
member val OnChangeCallback : bool -> unit = ignore with get, set
member this.SetControlledValue(v) =
this.ControlledState <- Writing
this.IsExpanded <- v
this.ControlledState <- Listening
// For uncontrolled changes to the property, this handler resets the property
// to the old value, and forwards the new value to the change callback.
override this.OnIsExpandedChanged(e) =
if this.ControlledState = Listening then
this.ControlledState <- Resetting
this.IsExpanded <- e.OldValue :?> bool
this.ControlledState <- Listening
e.NewValue :?> bool |> this.OnChangeCallback
else if this.ControlledState = Writing then
base.OnIsExpandedChanged(e)
@B-Reif
Copy link
Author

B-Reif commented Jan 25, 2021

In this case the framework invokes SetControlledValue so that the controlled changes aren't intercepted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment