Skip to content

Instantly share code, notes, and snippets.

@emoacht
emoacht / SKHelper.cs
Last active July 25, 2023 23:54
Get BitmapImage from SKImage of SkiaSharp
using System.IO;
using System.Windows.Media.Imaging;
using SkiaSharp;
public static class SKHelper
{
public static BitmapImage Convert(string filePath)
{
using SKImage image = SKImage.FromEncodedData(filePath);
using SKData encoded = image.Encode(); // PNG format
@emoacht
emoacht / ItemsControlBehavior.cs
Created July 7, 2023 00:01
Behavior to capture the change of source collection.
using System;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using Microsoft.Xaml.Behaviors;
public class ItemsControlBehavior : Behavior<ItemsControl>
{
@emoacht
emoacht / DialogHelper.cs
Created May 20, 2023 09:47
Use WinRT's FileOpenPicker and FolderPicker from WPF.
using System;
using System.Threading.Tasks;
using System.Windows.Interop;
using Windows.Storage.Pickers;
public static async Task<string?> OpenFileDialog(System.Windows.Window window)
{
var picker = new FileOpenPicker()
{
ViewMode = PickerViewMode.Thumbnail
@emoacht
emoacht / MainWindow1.xaml
Created April 29, 2023 21:31
Sample Styles of Round Button
<Window x:Class="WpfApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
Width="400" Height="200">
<Window.Resources>
<Style x:Key="RoundStyle" TargetType="{x:Type Button}">
<Setter Property="Width" Value="100"/>
<Setter Property="Height" Value="100"/>
<Setter Property="Foreground">
@emoacht
emoacht / DependencyObjectExtensions.cs
Created April 15, 2023 02:30
Scroll ScrollViewer of ListBox by mouse wheel.
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Windows;
using System.Windows.Media;
public static class DependencyObjectExtensions
{
public static bool TryFindDescendant<T>(this DependencyObject reference, [NotNullWhen(true)] out T? descendant) where T : DependencyObject
{
var queue = new Queue<DependencyObject>();
@emoacht
emoacht / TaskbarHelper.cs
Created April 14, 2023 02:25
Switch taskbar aute hide state.
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
internal static class TaskbarHelper
{
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
extern static IntPtr FindWindow(
[MarshalAs(UnmanagedType.LPWStr), In] string lpClassName,
[MarshalAs(UnmanagedType.LPWStr), In] string? lpWindowName);
@emoacht
emoacht / MainWindow.xaml.cs
Last active March 25, 2023 03:51
Generate and consume IAsyncEnumerable<T>.
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.Loaded += OnLoaded;
}
private async void OnLoaded(object sender, RoutedEventArgs e)
{
@emoacht
emoacht / MonitorHelper.cs
Created January 17, 2023 12:44
Enumerate monitor connections by WMI.
using System.Collections.Generic;
using System.Linq;
using System.Management;
public static class MonitorHelper
{
public static IEnumerable<MonitorConnection> EnumerateMonitorConnections()
{
var @class = new ManagementClass(@"root\wmi:WmiMonitorConnectionParams");
@emoacht
emoacht / CollectionToBooleanConverter.cs
Created January 5, 2023 01:18
Converts the state whether the source object is a collection and it has any item to boolean.
using System;
using System.Collections;
using System.Globalization;
using System.Windows.Data;
public class CollectionToBooleanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is IEnumerable collection)
@emoacht
emoacht / WindowHelper.cs
Last active December 6, 2022 20:23
Test alternative PointToScreen and ScreenToPoint methods.
using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;
public static class WindowHelper
{
public static Point PointToScreen(Visual visual, Point point)
{