5.8 KiB
source_files, generated_at, model, schema_version, sha256
| source_files | generated_at | model | schema_version | sha256 | |
|---|---|---|---|---|---|
|
2026-04-16T11:10:12.391241+00:00 | zai-org/GLM-5-FP8 | 1 | 001b39a3411ec3ba |
Documentation: VirtualToggleButton.cs
1. Purpose
VirtualToggleButton is a static utility class that provides attached dependency properties to imbue any WPF element with toggle button behavior without requiring inheritance from ToggleButton. It enables elements to respond to mouse clicks and keyboard input (Space/Enter) to toggle between checked/unchecked/indeterminate states, and raises the corresponding routed events. This is useful for templated controls or custom elements that need toggle semantics without the overhead of full ToggleButton derivation.
2. Public Interface
Attached Properties
| Property | Type | Default | Metadata Flags |
|---|---|---|---|
IsLockedProperty |
Nullable<bool> |
false |
BindsTwoWayByDefault, Journal |
IsThreeStateProperty |
bool |
false |
None |
IsVirtualToggleButtonProperty |
bool |
false |
None |
Public Methods
GetIsLocked(DependencyObject d) -> Nullable<bool>
Retrieves the current locked/checked state of the target element.
SetIsLocked(DependencyObject d, Nullable<bool> value)
Sets the locked/checked state. Setting to true raises ToggleButton.CheckedEvent; false raises ToggleButton.UncheckedEvent; null raises ToggleButton.IndeterminateEvent.
GetIsThreeState(DependencyObject d) -> bool
Returns whether the target supports three-state behavior (true/false/null).
SetIsThreeState(DependencyObject d, bool value)
Enables or disables three-state mode. When true, IsLocked can be set to null as a third state.
GetIsVirtualToggleButton(DependencyObject d) -> bool
Returns whether the target is currently acting as a virtual toggle button.
SetIsVirtualToggleButton(DependencyObject d, bool value)
When set to true, attaches MouseLeftButtonDown and KeyDown handlers to the target (must implement IInputElement). When set to false, detaches these handlers.
Internal Methods
RaiseCheckedEvent(UIElement target) -> RoutedEventArgs
Raises ToggleButton.CheckedEvent on the target element. Returns null if target is null.
RaiseUncheckedEvent(UIElement target) -> RoutedEventArgs
Raises ToggleButton.UncheckedEvent on the target element. Returns null if target is null.
RaiseIndeterminateEvent(UIElement target) -> RoutedEventArgs
Raises ToggleButton.IndeterminateEvent on the target element. Returns null if target is null.
3. Invariants
-
Two-way binding by default:
IsLockedPropertyis registered withBindsTwoWayByDefault, so bindings will update the source automatically. -
Event attachment requirement:
IsVirtualToggleButtononly attaches event handlers if the target implementsIInputElement. Non-IInputElementtargets are silently ignored. -
Event raising requirement:
RaiseEvent()only works for targets that are eitherUIElementorContentElement. OtherDependencyObjecttypes will not raise events. -
Toggle cycle behavior:
- When
IsThreeStateisfalse: toggles betweentrueandfalseonly. - When
IsThreeStateistrue: cycles throughfalse→true→null→false.
- When
-
Keyboard handling constraints:
- Space key is ignored when Alt modifier is present (to avoid interfering with system menu).
- Enter key only triggers toggle if
KeyboardNavigation.AcceptsReturnPropertyistrueon the sender.
-
Event source filtering:
OnKeyDownonly processes events wheree.OriginalSource == sender, preventing handling of bubbled events from child elements.
4. Dependencies
This module depends on:
System(core types)System.Windows(DependencyObject,DependencyProperty,FrameworkPropertyMetadata,UIElement,ContentElement,RoutedEventArgs,IInputElement)System.Windows.Controls.Primitives(ToggleButton- forCheckedEvent,UncheckedEvent,IndeterminateEvent)System.Windows.Input(Key,KeyEventArgs,Keyboard,KeyboardNavigation,ModifierKeys,MouseButtonEventArgs)
Consumers:
- Unclear from source alone. Any XAML or code in the
DTS.Viewer.GraphListnamespace or referencing it may use these attached properties.
5. Gotchas
-
Misleading XML documentation: The XML comments for
IsLockedstate "indicates whether the toggle button is checked" — this appears to be copy-pasted from a standardToggleButtonimplementation. The property is namedIsLocked, notIsChecked, which may indicate domain-specific semantics (e.g., locking a graph item vs. selecting it). -
Type mismatch between event raising methods:
RaiseCheckedEvent,RaiseUncheckedEvent, andRaiseIndeterminateEventaccept onlyUIElement, but the privateRaiseEventhelper supports bothUIElementandContentElement. If aContentElementhasIsLockedset, the change handler will attempt to raise events on it viaRaiseEvent, but the publicRaiseXxxEventmethods cannot be called withContentElementdirectly. -
Null return from event methods: The
RaiseXxxEventmethods returnnullwhen the target is null, but they do not throw. Callers should check for null if they intend to use the returnedRoutedEventArgs. -
Local variable shadows property name: In
UpdateIsLocked(), the local variable is namedIsLocked, which shadows the property accessor method naming convention. This is legal but could cause confusion during debugging. -
No validation of IsThreeState consistency: Setting
IsLockedtonullis always possible regardless ofIsThreeStatevalue. TheIsThreeStateproperty only affects toggle behavior inUpdateIsLocked(), not direct programmatic assignment.