Skip to content

Instantly share code, notes, and snippets.

<Window x:Class="WpfApplication4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" DataContext="{Binding RelativeSource={RelativeSource Self}}"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<ComboBox ItemsSource="{Binding Items}" SelectedValue="{Binding Selected}"
class Program
{
private static Action<string> _saved;
static void Main(string[] args)
{
M();
M();
Console.WriteLine("Back in Main()...");
Console.ReadKey();
@ivan-danilov
ivan-danilov / gist:393c06febbe4ab4203f5
Last active December 29, 2015 18:03
DependencyProperty changed event
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Data;
namespace Utilities
{
public static class DependencyPropertyExtensions
{
/// <summary>
/// Executes passed task (probably result of async method call) on a Dispatcher as
/// synchronous one, but without Wait() and risk of deadlock, using new dispatcher frame
/// that is finished as a continuation of passed task.
///
/// You should still await the task returned back if you want correct exception propagation.
///
/// Note that you can execute this method only if you're in a WPF dispatcher's context,
/// otherwise it will throw an exception.
/// </summary>
var a = new StrongBox<object>(new object());
var w = new WeakReference(a.Value);
a.Value = null;
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
Console.WriteLine(w.IsAlive ? "Alive" : "Dead");
Console.ReadLine();
@ivan-danilov
ivan-danilov / DataVirtualizedItemAutomationPeer.cs
Last active August 29, 2015 14:21
virtualized peer in WPF
/// <summary>
/// Represents AutomationPeer for certain data item. As data item is not control or even FrameworkElement,
/// it delegates most of its properties to the wrapping container item (generated by ItemContainerGenerator
/// as part of UI virtualization process).
/// </summary>
/// <remarks>
/// Most of the time automation client will work with this peer. It saves data item and not container because
/// container could be virtualized and then recycled. With this peer, client is safe from such details, as for
/// each operation we will get the container that currently wraps requested data item first and ask it for
/// corresponding property. Hence client does not need to worry about UI virtualization implementation details.
@ivan-danilov
ivan-danilov / p.hs
Created November 16, 2014 22:44
Haskell simple expr parser
import Data.Char
import Control.Monad
newtype Parser a = P (String -> [(a, String)])
parse :: Parser a -> String -> [(a, String)]
parse (P p) str = p str
instance Monad Parser where
return v = P (\inp -> [(v, inp)])
public class HiddenMsgOnlyWindow : IDisposable
{
private delegate IntPtr WndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private struct WNDCLASS
{
public uint style;
public IntPtr lpfnWndProc;
public int cbClsExtra;
@ivan-danilov
ivan-danilov / EventHandlerUtils.cs
Created February 27, 2012 11:57
Non-conforming weak delegates taming
using System;
using System.Diagnostics;
using System.Reflection;
namespace CommonTypes.Utilities.WeakDelegates
{
public delegate void UnregisterCallback<TEventArgs>(EventHandler<TEventArgs> eventHandler)
where TEventArgs : EventArgs;
public static class EventHandlerUtils
@ivan-danilov
ivan-danilov / InfiniteWriter.cs
Created December 16, 2011 23:21
Experiments with stream redirection
class Program
{
static void Main(string[] args)
{
try
{
int i = 0;
while (true)
{
Console.WriteLine("Line {0}", i++);