Class ColorPickerButton
A swatch-preview button that opens a full ColorPicker in a popup.
public class ColorPickerButton : Control, INotifyPropertyChanging, IBindingTarget, INotifyPropertyChanged, INotifyFocusChanged, IContainerLayout
- Inheritance
-
ColorPickerButton
- Implements
- Inherited Members
- Extension Methods
Remarks
Mirrors Selector's own relationship to ToggleButton exactly: this class derives from Control (not ToggleButton) and composes an internal ToggleButton as Chrome's child, since this control owns popup-lifecycle/IsOpen/ SelectedColor state that doesn't belong on ToggleButton itself. The popup is a AddOverlay(UIElement)/RemoveOverlay(UIElement) non-modal popup anchored under the button, with outside-click dismiss - the exact mechanism Selector already established for Dropdown/ComboBox, reused wholesale here.
Not currently template-safe (see Template's own remarks), for the same reason as ColorPicker - the internal ToggleButton is composed directly into Chrome once, at construction, with no OnApplyTemplate() override to re-wire it. Setting Template on a ColorPickerButton silently orphans this whole visual tree instead of throwing.
Constructors
ColorPickerButton()
Initializes a new instance of the ColorPickerButton class.
public ColorPickerButton()
Properties
IsOpen
Gets or sets a value indicating whether the popup is currently shown.
public bool IsOpen { get; set; }
Property Value
SelectedColor
Gets or sets the currently picked color.
[BindingOverride(BindingMode.TwoWay)]
public Color SelectedColor { get; set; }
Property Value
SwatchColors
Gets the caller-supplied palette shown in the popup's "Swatches" tab.
public IList<Color> SwatchColors { get; }
Property Value
Remarks
Forwards directly to SwatchColors - call this class's own RefreshSwatches() after mutating this collection (the internal ColorPicker instance is private, so its own RefreshSwatches() isn't reachable from outside this class).
Methods
OnArrangeUpdated()
Raises the ArrangeUpdated event.
protected override void OnArrangeUpdated()
Remarks
Re-positions an open popup, so it stays attached when this control moves - e.g. after a window resize or a EffectiveScale change re-lays out the surface. The reposition is deferred until every root element is arranged (this control's ancestors are still mid-arrange here) and runs before the canvas arranges its overlays, so the popup lands in the right place within the same frame.
OnAttached()
Raises the Attached event.
protected override void OnAttached()
Remarks
Propagates Canvas to every element in GetVisualChildren() - this is what makes attaching a whole pre-built subtree (the common pattern: build children, then attach the root once via Add(UIElement)) reach every descendant, not just the immediate root. Each child's own Canvas setter recurses into its children the same way, so this walks the full subtree regardless of depth. Container-specific child-add paths (e.g. OnChildAdded(object?, GenericEventArgs<UIElement>)) still assign Canvas directly for children added after the parent is already attached - this cascade is what covers everything built beforehand.
OnDetached()
Raises the Detached event.
protected override void OnDetached()
Remarks
Propagates null to every element in GetVisualChildren(), mirroring OnAttached() - see its remarks.
RefreshSwatches()
Rebuilds the popup's internal ColorPicker's "Swatches" tab's buttons from the current SwatchColors.
public void RefreshSwatches()
Events
ColorChanged
Occurs when SelectedColor changes.
public event EventHandler? ColorChanged