Skip to content

Instantly share code, notes, and snippets.

@emoacht
emoacht / StoreHelperOld.cs
Created June 3, 2022 13:52
Set owner window before .NET 5.0.
[ComImport]
[Guid("3E68D4BD-7135-4D10-8018-9FB6D9F33FA1")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IInitializeWithWindow
{
void Initialize(IntPtr hwnd);
}
public void SetOwnerWindow(StoreContext context, Window window)
{
@emoacht
emoacht / StoreHelper.cs
Created June 3, 2022 13:25
Update package by Windows.Services.Store API on .NET 5.0.
using System;
using System.Net.NetworkInformation;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Interop;
using Windows.Services.Store;
using WinRT.Interop;
internal static class StoreHelper
{
@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);
}