xiaohongshu
xiaohongshu
handling-wpf-input-commands
Implements WPF input handling with RoutedCommand, ICommand, CommandBinding, and InputBinding patterns. Use when creating keyboard shortcuts, menu commands, or custom command implementations.
Full skill instructions
Handling user input and implementing command patterns in WPF applications.
ICommand (Interface)
├── RoutedCommand (WPF built-in)
│ ├── ApplicationCommands (Copy, Paste, Cut, etc.)
│ ├── NavigationCommands (BrowseBack, BrowseForward, etc.)
│ ├── MediaCommands (Play, Pause, Stop, etc.)
│ └── EditingCommands (ToggleBold, ToggleItalic, etc.)
└── RelayCommand / DelegateCommand (MVVM)
<Window.CommandBindings>
<CommandBinding Command="ApplicationCommands.Copy"
Executed="CopyCommand_Executed"
CanExecute="CopyCommand_CanExecute"/>
<CommandBinding Command="ApplicationCommands.Paste"
Executed="PasteCommand_Executed"/>
</Window.CommandBindings>
<StackPanel>
<Button Command="ApplicationCommands.Copy" Content="Copy (Ctrl+C)"/>
<Button Command="ApplicationCommands.Paste" Content="Paste (Ctrl+V)"/>
<Button Command="ApplicationCommands.Undo" Content="Undo (Ctrl+Z)"/>
<Button Command="ApplicationCommands.Redo" Content="Redo (Ctrl+Y)"/>
</StackPanel>
private void CopyCommand_Executed(object sender, ExecutedRoutedEventArgs e)
{
// Copy logic
Clipboard.SetText(SelectedText);
}
private void CopyCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
// Enable condition
e.CanExecute = !string.IsNullOrEmpty(SelectedText);
}
| Category | Commands |
|---|---|
| ApplicationCommands | New, Open, Save, SaveAs, Close, Print, Copy, Cut, Paste, Undo, Redo, Find, Replace, SelectAll, Delete, Properties, Help |
| NavigationCommands | BrowseBack, BrowseForward, BrowseHome, BrowseStop, Refresh, Favorites, Search, GoToPage, NextPage, PreviousPage, FirstPage, LastPage |
| MediaCommands | Play, Pause, Stop, Record, NextTrack, PreviousTrack, FastForward, Rewind, ChannelUp, ChannelDown, TogglePlayPause, IncreaseVolume, DecreaseVolume, MuteVolume |
| EditingCommands | ToggleBold, ToggleItalic, ToggleUnderline, IncreaseFontSize, DecreaseFontSize, AlignLeft, AlignCenter, AlignRight, AlignJustify |
namespace MyApp.Commands;
using System.Windows.Input;
public static class CustomCommands
{
// Define custom command
public static readonly RoutedCommand RefreshData = new(
nameof(RefreshData),
typeof(CustomCommands),
new InputGestureCollection
{
new KeyGesture(Key.F5),
new KeyGesture(Key.R, ModifierKeys.Control)
});
public static readonly RoutedCommand ExportToPdf = new(
nameof(ExportToPdf),
typeof(CustomCommands),
new InputGestureCollection
{
new KeyGesture(Key.E, ModifierKeys.Control | ModifierKeys.Shift)
});
public static readonly RoutedCommand ToggleFullScreen = new(
nameof(ToggleFullScreen),
typeof(CustomCommands),
new InputGestureCollection
{
new KeyGesture(Key.F11)
});
}
<Window xmlns:cmd="clr-namespace:MyApp.Commands">
<Window.CommandBindings>
<CommandBinding Command="{x:Static cmd:CustomCommands.RefreshData}"
Executed="RefreshData_Executed"
CanExecute="RefreshData_CanExecute"/>
<CommandBinding Command="{x:Static cmd:CustomCommands.ExportToPdf}"
Executed="ExportToPdf_Executed"/>
</Window.CommandBindings>
<Menu>
<MenuItem Header="_File">
<MenuItem Header="_Refresh"
Command="{x:Static cmd:CustomCommands.RefreshData}"
InputGestureText="F5"/>
<MenuItem Header="_Export to PDF"
Command="{x:Static cmd:CustomCommands.ExportToPdf}"
InputGestureText="Ctrl+Shift+E"/>
</MenuItem>
</Menu>
</Window>
<Window.InputBindings>
<!-- Keyboard shortcut -->
<KeyBinding Key="N" Modifiers="Control" Command="ApplicationCommands.New"/>
<KeyBinding Key="S" Modifiers="Control" Command="ApplicationCommands.Save"/>
<KeyBinding Key="S" Modifiers="Control+Shift" Command="ApplicationCommands.SaveAs"/>
<KeyBinding Key="F4" Modifiers="Alt" Command="ApplicationCommands.Close"/>
<!-- Function keys -->
<KeyBinding Key="F1" Command="ApplicationCommands.Help"/>
<KeyBinding Key="F5" Command="{x:Static cmd:CustomCommands.RefreshData}"/>
<!-- MVVM Command binding -->
<KeyBinding Key="Delete" Command="{Binding DeleteCommand}"/>
</Window.InputBindings>
<Border.InputBindings>
<!-- Double-click -->
<MouseBinding MouseAction="LeftDoubleClick"
Command="{Binding OpenDetailCommand}"/>
<!-- Middle click -->
<MouseBinding MouseAction="MiddleClick"
Command="{Binding CloseTabCommand}"/>
<!-- Ctrl + Left click -->
<MouseBinding MouseAction="LeftClick"
Modifiers="Control"
Command="{Binding MultiSelectCommand}"/>
</Border.InputBindings>
<Button Command="{Binding NavigateCommand}"
CommandParameter="Home"
Content="Go to Home"/>
<Button Command="{Binding SetZoomCommand}"
CommandParameter="100"
Content="Zoom 100%"/>
<ListBox x:Name="ItemList" ItemsSource="{Binding Items}"/>
<Button Command="{Binding DeleteCommand}"
CommandParameter="{Binding SelectedItem, ElementName=ItemList}"
Content="Delete Selected"/>
<!-- Self-reference -->
<Button Command="{Binding ProcessCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource Self}}"
Content="Process This Button"/>
<StackPanel>
<TextBox x:Name="TargetTextBox"/>
<!-- Commands target the TextBox even when button is focused -->
<Button Command="ApplicationCommands.Copy"
CommandTarget="{Binding ElementName=TargetTextBox}"
Content="Copy from TextBox"/>
<Button Command="ApplicationCommands.Paste"
CommandTarget="{Binding ElementName=TargetTextBox}"
Content="Paste to TextBox"/>
</StackPanel>
namespace MyApp.Views;
using System.Windows;
using System.Windows.Input;
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
// Preview events (Tunneling - captured before child elements)
PreviewKeyDown += OnPreviewKeyDown;
// Normal events (Bubbling - captured after child elements)
KeyDown += OnKeyDown;
}
private void OnPreviewKeyDown(object sender, KeyEventArgs e)
{
// Global shortcut handling
if (e.Key == Key.Escape)
{
// Close popup or cancel operation
e.Handled = true;
}
// Modifier key combinations
if (Keyboard.Modifiers == ModifierKeys.Control && e.Key == Key.G)
{
// Ctrl+G: Go to line
ShowGoToLineDialog();
e.Handled = true;
}
}
private void OnKeyDown(object sender, KeyEventArgs e)
{
// Handle if not processed by child elements
}
}
private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
// Allow only numeric input
if (!char.IsDigit(e.Text, 0))
{
e.Handled = true;
}
}
namespace MyApp.Controls;
using System.Windows;
using System.Windows.Input;
public partial class DrawingCanvas : FrameworkElement
{
private Point _startPoint;
private bool _isDragging;
public DrawingCanvas()
{
MouseLeftButtonDown += OnMouseLeftButtonDown;
MouseMove += OnMouseMove;
MouseLeftButtonUp += OnMouseLeftButtonUp;
MouseWheel += OnMouseWheel;
}
private void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
_startPoint = e.GetPosition(this);
_isDragging = true;
// Capture mouse to receive events outside the element
CaptureMouse();
// Click count detection
if (e.ClickCount == 2)
{
// Double click
}
}
private void OnMouseMove(object sender, MouseEventArgs e)
{
if (!_isDragging) return;
var currentPoint = e.GetPosition(this);
var delta = currentPoint - _startPoint;
// Draw or drag logic
}
private void OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (_isDragging)
{
_isDragging = false;
ReleaseMouseCapture();
}
}
private void OnMouseWheel(object sender, MouseWheelEventArgs e)
{
// e.Delta: positive = scroll up, negative = scroll down
var zoomFactor = e.Delta > 0 ? 1.1 : 0.9;
ApplyZoom(zoomFactor);
}
}
// Set focus
myTextBox.Focus();
// Set keyboard focus specifically
Keyboard.Focus(myTextBox);
// Check focus
if (myTextBox.IsFocused)
{
// Has focus
}
if (myTextBox.IsKeyboardFocused)
{
// Has keyboard focus
}
<!-- Set default focused element -->
<Window FocusManager.FocusedElement="{Binding ElementName=FirstTextBox}">
<StackPanel>
<TextBox x:Name="FirstTextBox"/>
<TextBox x:Name="SecondTextBox"/>
</StackPanel>
</Window>
<!-- Define focus scope -->
<ToolBar FocusManager.IsFocusScope="True">
<Button Content="Button 1"/>
<Button Content="Button 2"/>
</ToolBar>
xiaohongshu
technical spec
product ux expert
database patterns
Conduct multi-agent task orchestration and workflow coordination.
Initialize project with Conductor artifacts (product definition,
Expert in web animations, transitions, and motion design using Framer Motion and CSS
Creates Mermaid and ASCII diagrams for flowcharts, architecture, ERDs, state machines, mindmaps, and more. Use when user mentions diagram, flowchart, mermaid, ASCII diagram, text diagram, terminal diagram, visualize, C4, mindmap, architecture diagram, sequence diagram, ERD, or needs visual docume...
PostgreSQL bindings for H3 hexagonal grid system. Use when working with H3 cells in Postgres, including spatial indexing, geometry/geography integration, and raster analysis.
Context-Driven Development skill for projects using Conductor. Use this skill when you detect a `conductor/` directory in the project, when working on tasks defined in a `plan.md` file, or when the user asks about tracks, specs, or plans. Automatically applies TDD workflow, tracks task completion...
Display project status, active tracks, and next actions
Official Stakpak application containerization standard operating procedure, a step-by-step guidline to properly dockerize applications. This is a rule book curated by the Stakpak Team.
Generate, edit, and beat-sync AI video with leading models in one workspace.
The world's fastest calendar for remote work
Transform Your Design with AI Designer by ImgCreator.ai
Revolutionizing Video Production with AI-Powered Creativity
Extend an image past the frame and let AI fill the new aspect ratio.
Discover your celebrity doppelgänger with StarByFace!
ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.
Opus.ai: Revolutionize Your Web Experience