Skip to content

Instantly share code, notes, and snippets.

@emoacht
emoacht / BooleanToBrushConverter.cs
Created May 29, 2022 08:31
Test bindings by nested properties through Border.Child and Border.Parent.
[ValueConversion(typeof(bool), typeof(Brush))]
public class BooleanToBrushConverter : IValueConverter
{
public Brush TrueBrush { get; set; } = Brushes.Gray;
public Brush FalseBrush { get; set; } = Brushes.Gray;
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value switch
{
@emoacht
emoacht / WindowCenterMover.cs
Created May 20, 2022 12:15
Move a window at the center of monitor.
using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
public class WindowCenterMover
{
#region Win32
[DllImport("User32.dll")]
@emoacht
emoacht / NotifyIconHolder.cs
Created May 15, 2022 15:23
Hold and show NotifyIcon from WPF app.
using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Forms;
public class NotifyIconHolder
{
public event EventHandler<Point>? MouseRightClick;
public event EventHandler<Point>? MouseLeftClick;
public event EventHandler<Point>? MouseDoubleClick;
@emoacht
emoacht / PopupHelper.cs
Created April 29, 2022 11:32
Get relative position of Popup to its parent.
public static class PopupHelper
{
[DllImport("User32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
[StructLayout(LayoutKind.Sequential)]
private struct RECT
{
public int Left;
@emoacht
emoacht / Program.cs
Created April 10, 2022 06:16
Show all supported cultures.
using System;
using System.Globalization;
public class Program
{
static void Main(string[] args)
{
foreach (var c in CultureInfo.GetCultures(CultureTypes.AllCultures))
Console.WriteLine(c);
@emoacht
emoacht / ContextMenuHelper.cs
Created April 9, 2022 15:50
Get relative position of ContextMenu from target.
public static class ContextMenuHelper
{
public static Point GetRelativePositionFromTarget(this ContextMenu source)
{
return source.TranslatePoint(new Point(0, 0), source.PlacementTarget);
}
}
@emoacht
emoacht / TreeViewHelper.cs
Created March 30, 2022 00:41
Attached property to move a part of TreeViewItem to the edge of TreeView with a specified left margin
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
public static class TreeViewHelper
{
public static double? GetLeftMargin(DependencyObject obj)
{
return (double?)obj.GetValue(LeftMarginProperty);
}
@emoacht
emoacht / ListViewHelper.cs
Created March 23, 2022 04:16
Get index number of ListViewItem in ListView.
public class ListViewHelper
{
public static int GetIndex(object item)
{
if (item is not ListViewItem listViewItem)
return -1;
ListView listView = (ListView)ItemsControl.ItemsControlFromItemContainer(listViewItem);
return listView.ItemContainerGenerator.IndexFromContainer(listViewItem);
}
@emoacht
emoacht / WindowHelper.cs
Created March 21, 2022 06:18
Get the direction of avilable space in the monitor where a specified window locates.
using System;
using System.Runtime.InteropServices;
public enum Direction { None, TopLeft, TopRight, BottomRight, BottomLeft }
public static class WindowHelper
{
public static Direction GetAvailableDirection(IntPtr windowHandle)
{
if (!GetWindowRect(windowHandle, out RECT buffer))
@emoacht
emoacht / MainWindow.xaml.cs
Created March 19, 2022 14:07
Open URL by default browser or open folder by Explorer with Windows.System.Launcher methods
using System;
using System.Windows;
using Windows.System;
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}