Skip to content

Instantly share code, notes, and snippets.

@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();
}
@emoacht
emoacht / ContentControlBehavior.cs
Created March 14, 2022 05:58
Behavior for ContextMenu
using System.Windows;
using System.Windows.Controls;
using Microsoft.Xaml.Behaviors;
public class ContextMenuBehavior : Behavior<ContextMenu>
{
protected override void OnAttached()
{
base.OnAttached();
@emoacht
emoacht / MainWindow.cs
Created March 11, 2022 23:12
Compare Dispatcher.Invoke(Func<Task>) (returns Task) and Dispatcher.InvokeAsync (returns DispatcherOperation(Task)).
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
await Task.Run(async () =>
@emoacht
emoacht / ListViewStyle.xaml
Created March 6, 2022 00:14
Style for ListView for focus state. Note that usually, MultiDataTrigger is not necessary.
<Style x:Key="ListViewStyle" TargetType="{x:Type ListView}">
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Foreground" Value="Black"/>
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsFocused}" Value="True"/>
<Condition Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListView}}, Path=IsKeyboardFocusWithin}" Value="True"/>
@emoacht
emoacht / IdleTime.cs
Created January 23, 2022 14:07
Get system idle time.
public static class IdleTime
{
[DllImport("User32.dll")]
private static extern bool GetLastInputInfo(ref LASTINPUTINFO info);
private struct LASTINPUTINFO
{
public uint cbSize;
public uint dwTime;
}
@emoacht
emoacht / StripedProgressBar.xaml
Created December 27, 2021 06:18
Striped ProgressBar for WPF
<SolidColorBrush x:Key="ProgressBar.Background" Color="#FFE6E6E6"/>
<SolidColorBrush x:Key="ProgressBar.Progress" Color="#FF1C61F3"/>
<SolidColorBrush x:Key="ProgressBar.Stripe" Color="#33FFFFFF"/>
<Style x:Key="StripedProgressBarStyle" TargetType="{x:Type ProgressBar}">
<Setter Property="Background" Value="{StaticResource ProgressBar.Background}"/>
<Setter Property="Foreground" Value="{StaticResource ProgressBar.Progress}"/>
<Setter Property="Height" Value="20"/>
<Setter Property="Template">
<Setter.Value>
@emoacht
emoacht / MainWindow.xaml
Created December 26, 2021 06:42
Sample TextBox for font glyphs with symbols
<Window x:Class="WpfFontGlyph.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
SizeToContent="Height" Height="100" Width="480">
<StackPanel>
<Grid Margin="10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
@emoacht
emoacht / Program.cs
Created December 5, 2021 04:04
Results of pattern matching in terms of null
private class S
{
public bool Enabled { get; }
public S(bool enabled) => Enabled = enabled;
}
static void Main(string[] args)
{
M(new S(true));