Skip to content

Instantly share code, notes, and snippets.

View SchreinerK's full-sized avatar

Kay-Uwe Schreiner (kux) SchreinerK

View GitHub Profile
@SchreinerK
SchreinerK / Default.cs
Created April 18, 2023 15:45
Gets the default value for a Type.
public static class Utils {
public static object? Default(Type type) {
return Type.GetTypeCode(type) switch {
TypeCode.Boolean => false,
TypeCode.String => null,
TypeCode.Char => '\0',
TypeCode.SByte => (sbyte)0, TypeCode.Byte => (byte)0, TypeCode.Int16 => (short)0,
TypeCode.UInt16 => (ushort)0, TypeCode.Int32 => 0, TypeCode.UInt32 => (uint)0,
TypeCode.Int64 => (long)0, TypeCode.UInt64 => (ulong)0,
TypeCode.Single => 0.0f, TypeCode.Double => 0.0d, TypeCode.Decimal => 0m,
@SchreinerK
SchreinerK / PopupWindow.cs
Created December 2, 2022 10:09
Show Window on Mouse Position
public partial class PopupWindow : Window {
public PopupWindow() {
InitializeComponent();
WindowStartupLocation = WindowStartupLocation.Manual;
Loaded += (s, e) => { // HACK1: use Loaded
Mouse.Capture(this); // HACK2: use Capture
var p = PointToScreen(Mouse.GetPosition(this));
Mouse.Capture(null);
@SchreinerK
SchreinerK / CompareBinding.cs
Last active October 6, 2021 17:52
CompareBinding
// SOURCE: https://gist.github.com/SchreinerK/299093195aebf1d3ef0d413659255c0d
using System;
using System.Collections;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
// ReSharper disable once CheckNamespace
namespace KsWare.Presentation {
@SchreinerK
SchreinerK / CommandBindingMapper.cs
Last active October 1, 2021 10:26
KsWare.Presentaion.Input.CommandBindingMapper
// GIST: https://gist.github.com/SchreinerK/0aa450669ba0f66dbf821f92b15c1a66
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
using KsWare.Presentation.ViewFramework;
// ReSharper disable once CheckNamespace
namespace KsWare.Presentation.Input {
@SchreinerK
SchreinerK / AsyncHelper.cs
Created September 29, 2021 08:27
KsWare.Threading.AsyncHelper
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace KsWare.Threading {
public static class AsyncHelper {
public static Task<TResult> RunAsync<TResult>(Func<TResult> func) => Task.Run<TResult>(() => func());
@SchreinerK
SchreinerK / PropertyChangedDistributor.cs
Created November 26, 2020 14:33
PropertyChangedDistributor
using System;
using System.Collections.Generic;
using System.ComponentModel;
namespace Common {
public sealed class PropertyChangedDistributor: IDisposable {
private readonly INotifyPropertyChanged _obj;
private readonly Dictionary<string, List<Entry>> _subscriptions = new Dictionary<string, List<Entry>>();
@SchreinerK
SchreinerK / WrapPanelLineBreak.cs
Last active September 15, 2020 12:44
Line break in WrapPanel [WPF]
/// <summary>
/// Forces a new line in a <see cref="WrapPanel"/>
/// </summary>
public class WrapPanelLineBreak : FrameworkElement {
public WrapPanelLineBreak() {
Height = 0;
var binding = new Binding {
RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(WrapPanel), 1),
Path = new PropertyPath("ActualWidth")
};
@SchreinerK
SchreinerK / SharedResourceDictionary.Simple.cs
Last active April 22, 2020 09:08
SharedResourceDictionary Simple
using System;
using System.Collections.Generic;
using System.Windows;
namespace KsWare.Gist {
// https://gist.github.com/SchreinerK/a753cee33a0e791dea6779bce730eade
public class SharedResourceDictionary : ResourceDictionary {
private static readonly Dictionary<Uri, WeakReference> SharedCache = new Dictionary<Uri, WeakReference>();
@SchreinerK
SchreinerK / SharedResourceDictionary.cs
Last active April 22, 2020 08:39
SharedResourceDictionary
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
// https://gist.github.com/SchreinerK/3a886e7f2294fcc2e2fceceaae0967c6
namespace KsWare.Gist {
/// <summary>
/// The shared resource dictionary is a specialized resource dictionary
@SchreinerK
SchreinerK / DoubleCompareConverter.cs
Created March 23, 2020 10:52
DoubleCompareConverter
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
namespace Converter
{
public class DoubleCompareConverter : IValueConverter
{