Skip to content

Instantly share code, notes, and snippets.

@sachintha81
sachintha81 / ColorCodeDGV.xaml
Last active Dec 18, 2019
WPF + C# : Display Data and Color Code a DataGrid using Binding
View ColorCodeDGV.xaml
<Window x:Class="ColorLibrary.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"
xmlns:local="clr-namespace:ColorLibrary"
mc:Ignorable="d"
Loaded="Window_Loaded"
Title="MainWindow" Height="500" Width="400">
<Window.Resources>
@sachintha81
sachintha81 / WPFMessengerClass.cs
Created Jan 13, 2017
Messenger Class for the WPF + MVVM Pattern
View WPFMessengerClass.cs
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MVVMMessenger
{
public class Messenger
@sachintha81
sachintha81 / LoopEnum.cs
Last active Dec 6, 2019
Loop through Enum
View LoopEnum.cs
// Typed version
var values = Enum.GetValues(typeof(Foos)).Cast<Foos>();
// Helper function
public static class EnumUtil {
public static IEnumerable<T> GetValues<T>() {
return Enum.GetValues(typeof(T)).Cast<T>();
}
}
@sachintha81
sachintha81 / EqualityComparer.cs
Created Aug 9, 2017
Equality Comparer for comparing objects based on members
View EqualityComparer.cs
using System;
using System.Collections.Generic;
using System.Linq;
// SO Link 1 : https://stackoverflow.com/a/263416/302248
// SO Link 2 : https://stackoverflow.com/q/45601078/302248
namespace ComparerGetHashCode
{
public class NumberClass
{
@sachintha81
sachintha81 / MainWindow.xaml.cs
Last active Oct 20, 2019
WPF Progress Bar in a separate Window
View MainWindow.xaml.cs
using System;
using System.ComponentModel;
using System.Threading;
using System.Windows;
namespace WPFProgressBar
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
View ShuffleList.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StackOverflow
{
class Program
{
@sachintha81
sachintha81 / IntelligentPath.cs
Created Mar 26, 2018
Intelligent Path.Combine()
View IntelligentPath.cs
public static class Pathy
{
public static string Combine(string path1, string path2)
{
if (path1 == null) return path2
else if (path2 == null) return path1
else return path1.Trim().TrimEnd(System.IO.Path.DirectorySeparatorChar)
+ System.IO.Path.DirectorySeparatorChar
+ path2.Trim().TrimStart(System.IO.Path.DirectorySeparatorChar);
}
@sachintha81
sachintha81 / GetWeightedRandomNumber.cs
Created Oct 13, 2017
Get a Weighted Random Number
View GetWeightedRandomNumber.cs
public class Unit
{
public string Item { get; set; }
public int Weight { get; set; }
}
public class Program
{
public static Unit GetWeightedRandomNumber(List<Unit> list)
{
View CalculateAge.cs
public static void CalculateAge(DateTime dt1, DateTime dt2, out int years, out int months, out int days)
{
DateTime start = dt1 < dt2 ? dt1 : dt2;
DateTime end = dt2 > dt1 ? dt2 : dt1;
years = 0;
months = 0;
days = 0;
while (start.AddYears(1) < end)
@sachintha81
sachintha81 / GetEnumDescription.cs
Created Aug 16, 2017
Get the description of any Enum
View GetEnumDescription.cs
public static class EnumHelper<T>
{
public static string GetEnumDescription(string value)
{
Type type = typeof(T);
var name = Enum.GetNames(type).Where(f => f.Equals(value, StringComparison.CurrentCultureIgnoreCase)).Select(d => d).FirstOrDefault();
if (name == null)
{
return string.Empty;
You can’t perform that action at this time.