Chapter.Net.WPF 1.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Chapter.Net.WPF --version 1.1.0                
NuGet\Install-Package Chapter.Net.WPF -Version 1.1.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Chapter.Net.WPF" Version="1.1.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Chapter.Net.WPF --version 1.1.0                
#r "nuget: Chapter.Net.WPF, 1.1.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Chapter.Net.WPF as a Cake Addin
#addin nuget:?package=Chapter.Net.WPF&version=1.1.0

// Install Chapter.Net.WPF as a Cake Tool
#tool nuget:?package=Chapter.Net.WPF&version=1.1.0                

<img src="https://raw.githubusercontent.com/dwndland/Chapter.Net.WPF/master/Icon.png" alt="logo" width="64"/>

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

  1. Installation:

    • Install the Chapter.Net.WPF library via NuGet Package Manager:
    dotnet add package Chapter.Net.WPF
    
  2. 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>
    
  3. ControlFocus:

    • Usage.
    public class MyControl : Control
    {
        protected void OnGotFocus()
        {
            ControlFocus.GiveFocus(myButton);
        }
    }
    
  4. IconReader:

    Icon icon = new Icon
    {
        Source = IconReader.FileIcon("C:\\path\\Demo.exe", false, false)
    }
    
  5. 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();
        }
    }
    
  6. 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()
        {
        }
    }
    
  7. SystemTexts:

    • Load the translation for the "Cancel" button from windows.
    var cancelLabel = SystemTexts.GetString(SystemTexts.CANCEL_CAPTION);
    
  8. 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);
    }
    
  9. 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"));
        }
    }
    
  10. 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);
    
  11. 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();
    }
    
  12. 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)
        }
    }
    

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 Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net5.0-windows7.0 is compatible.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net6.0-windows7.0 is compatible.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net7.0-windows7.0 is compatible.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net8.0-windows7.0 is compatible. 
.NET Core netcoreapp3.0 is compatible.  netcoreapp3.1 was computed. 
.NET Framework net45 is compatible.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.

Version Downloads Last updated
3.0.0 88 6/7/2024
2.0.0 213 4/7/2024
1.1.0 307 3/31/2024
1.0.0 248 12/23/2023