Chapter.Net.WPF
1.0.0
See the version list below for details.
dotnet add package Chapter.Net.WPF --version 1.0.0
NuGet\Install-Package Chapter.Net.WPF -Version 1.0.0
<PackageReference Include="Chapter.Net.WPF" Version="1.0.0" />
paket add Chapter.Net.WPF --version 1.0.0
#r "nuget: Chapter.Net.WPF, 1.0.0"
// Install Chapter.Net.WPF as a Cake Addin #addin nuget:?package=Chapter.Net.WPF&version=1.0.0 // Install Chapter.Net.WPF as a Cake Tool #tool nuget:?package=Chapter.Net.WPF&version=1.0.0
Chapter.Net.WPF Library
Overview
Brings additional behavior and features to WPF in general.
Features
- BindingAdapter: Allows to modify a binding by using another bindings to their properties.
- ControlFocus: Brings an easy possibility to focus another UI control.
- IconReader: Provides a way to read file, extension and folder icons with and without caching.
- InputWatcher: Allows to listen for global keyboard or mouse actions without the app be in focus.
- PopupHandler: Implements an auto close event for custom WPF popups.
- SystemTexts: Loads translations for the current windows language from the system.
- Themeing: Checks if the system is on light or dark theme and allows setting it on a WPF window.
- UIDispatcher: Gives easy access to the current UI dispatcher and allows override for unit tests.
- VisualTreeAssist: Provides an easy way to seach for UI controls by any condition in any direction in an easy way.
- WindowHooks: Gives easy ways to hook into the WinAPI queue with custom callbacks.
- WindowObserver: Gives easy ways to WinAPI events on the current window.
Getting Started
Installation:
- Install the Chapter.Net.WPF library via NuGet Package Manager:
dotnet add package Chapter.Net.WPF
BindingAdapter:
- Bind a binding converter and bind a tool tip fallback value.
<TextBlock Text="{Binding Demo}" ToolTip="{Binding AnyTag}"> <Helpers:BindingAdapter.BindingExtensions> <Helpers:BindingExtensionCollection> <Helpers:BindingExtension Property="TextBlock.Text" Converter="{Binding DemoConverter}" ConverterParameter="{Binding DemoConverterParameter}" /> <Helpers:BindingExtension Property="TextBlock.ToolTip" FallbackValue="{Binding BindingFallbackValue}" /> </Helpers:BindingExtensionCollection> </Helpers:BindingAdapter.BindingExtensions> </TextBlock>
ControlFocus:
- Usage.
public class MyControl : Control { protected void OnGotFocus() { ControlFocus.GiveFocus(myButton); } }
IconReader:
Icon icon = new Icon { Source = IconReader.FileIcon("C:\\path\\Demo.exe", false, false) }
InputWatcher:
- Watch global keyboard events.
public class DemoClass : IDisposable { private SubscribeToken _token; public void Demo() { var watcher = new InputWatcher(); _token = watcher.Observe(new KeyboardInput(Key.Delete, ModifierKeys.Control, OnDeletePress)); watcher.Start(); //watcher.Stop(); } private void OnDeletePress(KeyboardEventArgs obj) { //obj.Key //obj.KeyPressState //obj.ModifierKeys } public void Dispose() { _token.Dispose(); } }
- Watch global mouse events.
public class DemoClass : IDisposable { private SubscribeToken _token; public void Demo() { var watcher = new InputWatcher(); _token = watcher.Observe(new MouseInput(MouseAction.RightDoubleClick, OnRightDoubleClick)); watcher.Start(); //watcher.Stop(); } private void OnRightDoubleClick(MouseEventArgs obj) { //obj.Modifiers } public void Dispose() { _token.Dispose(); } }
PopupHandler:
- Auto close a custom popup.
public class Control : ContentControl { private PopupHandler _popupHandler; public override void OnApplyTemplate() { var popup = GetTemplateChild("PART_Popup") as Popup; if (popup == null) return; _popupHandler = new PopupHandler(); _popupHandler.AutoClose(popup, OnPopupClosed); } private void OnPopupClosed() { } }
SystemTexts:
- Load the translation for the "Cancel" button from windows.
var cancelLabel = SystemTexts.GetString(SystemTexts.CANCEL_CAPTION);
Themeing:
- Set a WPF window to the current system theme (light or dark).
public void SetWindowTheme(Window window) { var currentTheme = ThemeManager.GetSystemTheme(); ThemeManager.SetWindowTheme(window, currentTheme); }
public void SetWindowTheme(Window window) { ThemeManager.SetWindowTheme(window, WindowTheme.System); }
UIDispatcher:
- Use the UI dispatcher and override on unit tests.
public void ViewModel : ObservableObject { private string _name; public string Name { get => _name; set => NotifyAndSetIfChanged(ref _name, value); } public void Update(string name) { UIDispatcher.Current.Invoke(() => Name = name); } }
[TestFixture] public class ViewModelTests { private ViewModel _target; [SetUp] public void Setup() { UIDispatcher.Override(Dispatcher.CurrentDispatcher); _target = new ViewModel(); } [Test] public void Update_Called_SetsTheProperty() { _target.Update("Peter"); Assert.That(_target.Name, Is.EqualTo("Peter")); } }
VisualTreeAssist:
- Find the first child button.
var childButton = VisualTreeAssist.FindChild<Button>(this);
- Find a child text box with a name.
var namedChildTextBox = VisualTreeAssist.FindNamedChild<TextBox>(this, "PART_TextBox");
- Find parent user control but stop at the current window.
var firstUserControlInWindow = VisualTreeAssist.GetParentsUntil<UserControl, Window>(this);
WindowHooks:
- Hook in to global keyboard events
public void HookIn() { var windowKeyboardHooks = new WindowHooks(); _windowKeyboardHooks.HookIn(process, WH.KEYBOARD_LL, KeyboardEventReceived); } private void KeyboardEventReceived(int code, IntPtr wParam, IntPtr lParam) { } public void HookOut() { _windowKeyboardHooks.HookOut(); }
WindowObserver:
- Do something if user double clicked the window title bar.
public partial class MainView { public MainView() { InitializeComponent(); var observer = new WindowObserver(this); observer.AddCallback(OnEventHappened); } private void OnEventHappened(NotifyEventArgs e) { if (e.MessageId == Chapter.Net.WinAPI.Data.WM.NCLBUTTONDBLCLK) { // User double clicked in the non client area (title bar most likely) } } }
public partial class MainView { public MainView() { InitializeComponent(); var observer = new WindowObserver(this); observer.AddCallbackFor(Chapter.Net.WinAPI.Data.WM.NCLBUTTONDBLCLK, OnEventHappened); } private void OnEventHappened(NotifyEventArgs e) { // User double clicked in the non client area (title bar most likely) } }
Links
License
Copyright (c) David Wendland. All rights reserved. Licensed under the MIT License. See LICENSE file in the project root for full license information.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0-windows7.0 is compatible. net7.0-windows was computed. net7.0-windows7.0 is compatible. net8.0-windows was computed. net8.0-windows7.0 is compatible. |
-
net6.0-windows7.0
- Chapter.Net.WinAPI (>= 1.0.0)
-
net7.0-windows7.0
- Chapter.Net.WinAPI (>= 1.0.0)
-
net8.0-windows7.0
- Chapter.Net.WinAPI (>= 1.0.0)
NuGet packages (3)
Showing the top 3 NuGet packages that depend on Chapter.Net.WPF:
Package | Downloads |
---|---|
Chapter.Net.WPF.Controls
A set of new WPF controls which are not yet build in. |
|
Chapter.Net.WPF.SystemTray
Chapter.Net.WPF.SystemTray brings objects and helper to work with the Windows system tray icon, its context menu and notifications. |
|
Chapter.Net.WPF.Theming
Brings features to work with the windows themes and accent colors under WPF. |
GitHub repositories
This package is not used by any popular GitHub repositories.