(Extension/WPF) Find Child from Parent Control by Name and Type

Find child control from a parent control using type and name. Method will return null if no control is found.

Usage:

Window mainWindow = new Window();
TextBlock testTb = mainWindow.FindChild<TextBlock>("testTextBlock");

Code:

using System.Windows;
using System.Windows.Media;

public static class FindChildControlFromParent
{
    public static T FindChild<T>(this DependencyObject parent, string childName)
        where T : DependencyObject
    {
        if (parent == null) return null;

        T foundChild = null;

        int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
        for (int i = 0; i < childrenCount; i++)
        {
            var child = VisualTreeHelper.GetChild(parent, i);
            T childType = child as T;
            if (childType == null)
            {
                foundChild = FindChild<T>(child, childName);

                if (foundChild != null) break;
            }
            else if (!string.IsNullOrEmpty(childName))
            {
                var frameworkElement = child as FrameworkElement;
                if (frameworkElement != null && frameworkElement.Name == childName)
                {
                    foundChild = (T)child;
                    break;
                }
            }
            else
            {
                foundChild = (T)child;
                break;
            }
        }

        return foundChild;
    }
}

(Converter) Reverse Boolean To Visibility Converter

This is Microsoft’s standard BooleanToVisibilityConverter but in reverse. This snippet can only be used in WPF.

public class ReverseBooleanToVisibilityConverter : IValueConverter
{

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var b = (bool)value;
        if (b)
        {
            return Visibility.Collapsed;
        }
        else
        {
            return Visibility.Visible;
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return false;
    }
}

(Converter) Reverse Boolean Converter

Converter for reversing a boolean value. This snippet is used in WPF.

public class ReverseBooleanConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (!(value is bool)) return value;
        return !(bool)value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

(Extension) Shuffle a List or an Array

This snippet will shuffle the content of your IList or Array.

Usage

IList<int> myList = new List<int>();
myList.Shuffle();

Snippet

using System;
using System.Collections.Generic;
public static class ListExtensions
{
	public static void Shuffle<T>(this IList<T> list)
	{
		Random rng = new Random();
		int n = list.Count;
		while (n > 1)
		{
		    n--;
		    int k = rng.Next(n + 1);
		    T value = list[k];
		    list[k] = list[n];
		    list[n] = value;
		}
	}

	public static void Shuffle<T>(this T[] list)
	{
		Random rng = new Random();
		int n = list.Length;
		while (n > 1)
		{
		    n--;
		    int k = rng.Next(n + 1);
		    T value = list[k];
		    list[k] = list[n];
		    list[n] = value;
		}
	}
}

(Extension) Compress / Uncompress byte[] using GzipStream

I use this code to easily compress a byte[] object.

Snippet:

// ########### USAGE ###########
// byte[] buffer = new byte[200];
// byte[] compressedBuffer = buffer.Compress();
// byte[] decompressedBuffer = compressedBuffer.Decompress();
// if (buffer == decompressedBuffer) Console.WriteLine("Matched");
// #############################

using System.IO.Compression;
using System.IO;

public static class ByteArrayGzipCompressionExtension
{
	public static byte[] Compress(this byte[] filetocompress)
	{
	    byte[] compressedFile;

	    using (var outputStream = new MemoryStream())
	    {
		using (var compressedStream = new GZipStream(outputStream, CompressionMode.Compress))
		{
		    using (var mStream = new MemoryStream(filetocompress))
		    {
			mStream.CopyTo(compressedStream);
		    }
		}

		compressedFile = outputStream.ToArray();
	    }

	    return compressedFile;
	}

	public static byte[] Decompress(this byte[] filetodecompress)
	{
	    byte[] decompressedFile;
	    using (var inStream = new MemoryStream(filetodecompress))
	    using (var bigStream = new GZipStream(inStream, CompressionMode.Decompress))
	    using (var bigStreamOut = new MemoryStream())
	    {
		bigStream.CopyTo(bigStreamOut);
		decompressedFile = bigStreamOut.ToArray();
		return decompressedFile;
	    }
	}
}