Struct CommandContext
Provides context for a Command invocation.
public readonly struct CommandContext : ICommandContext, IEquatable<CommandContext>
- Implements
- Inherited Members
- Extension Methods
Remarks
CommandContext is immutable. Use WithCommand(Command) or WithRouting(CommandRouting) to create a new context with modified values while preserving all other fields.
Use pattern matching to access specific binding types:
<pre><code class="lang-csharp">if (ctx.Binding is KeyBinding kb) { /* key input */ }
else if (ctx.Binding is MouseBinding mb) { /* mouse input / } else if (ctx.Binding is CommandBinding ib) { / programmatic */ }
Constructors
- CommandContext(Command, WeakReference<View>?, ICommandBinding?)
Initializes a new instance with the specified Command.
Properties
- Binding
The binding that triggered the command.
- Routing
Gets the routing mode for this command invocation.
- Source
A weak reference to the View that was the source of the command invocation, if any. (e.g. the view the user clicked on or the view that had focus when a key was pressed). Use
Source?.TryGetTarget(out View? view)to safely access the source view.
- Value
Gets the most recently appended value from Values, or null if Values is empty. This is a convenience accessor equivalent to
Values.Count > 0 ? Values[^1] : null.
- Values
Gets all values accumulated as the command propagated up the view hierarchy. Each IValue-implementing view in the chain appends its value via GetValue() as the command bubbles up. The list is ordered from innermost (originator) to outermost (last composite to append).
Methods
- WithCommand(Command)
Creates a new context with a different command, preserving all other fields.
- WithRouting(CommandRouting)
Creates a new context with different routing, preserving all other fields.
- WithValue(object?)
Creates a new context with the specified value appended to the Values chain. The new value becomes the last element, making it the new Value.