Schlagwort: C#

Created with Sketch.

Listening to Changes of DependencyProperties – DependencyPropertyWatcher

Dependency properties are widely used in WPF controls. However there is no buid-in way to listen to changes of such properties. The following class is a generic watcher for dependecy properties: public class DependencyPropertyWatcher<T> : DependencyObject, IDisposable { /// <summary> /// Value of Value Property /// </summary> public static readonly DependencyProperty ValueProperty = DependencyProperty.Register( „Value“,…

Extending the Random Class

The following code contains extension methods for the random class. One method will create random dates between a given minimum and maximum. The second method creates boolean random values. public static class RandomNumbers { public static DateTime NextDate(this Random r, DateTime minDate, DateTime maxDate) { var ticks = maxDate.Ticks – minDate.Ticks; if (ticks > Int32.MaxValue)…

Model-View-ViewModel (MVVM) Introduction

This post gives you an overview of the MVVM (Model-View-ViewModel) pattern. There are many similar very good articles like this, giving you a rough overview, historical background and/or nitty-gritty details on the pattern. Here I’d like to show you the very basics of the MVVM. Consider this as my personal view. Many of the other…

Creating a WPF Spinner Control

In this post I will show how to create a spinning progress control in WPF. This is often used to notify the user that a long-running operation is still in progress, without without knowing the current or remaining progress. The result looks like this and will turn infinitely: First of all we create a new…

Strip BOM from a File or Text

The following code removes the BOM (Byte Order Mark) from texts. Some text editors add that while saving files. Sometimes this leads to problems while reading or converting text files (e.g. XML files). The code removes the BOM and Zero White Spaces from the string and returns the clean string. /// <summary> /// Strips the…

Check if Code is Executed at Design Time

Sometimes it is important to prevent certain code being executed at design time. public static bool IsDesignTime { get { return (System.ComponentModel.LicenseManager.UsageMode == System.ComponentModel.LicenseUsageMode.Designtime); } } In WPF applications the following code can be used instead: public static bool IsDesignTime { get { System.ComponentModel.DesignerProperties.GetIsInDesignMode(new DependencyObject()); } }  

Obtain information about the Executing Assembly

Path of the executing assembly: public static string GetExecutingAssemblyPath() { string codeBase = Assembly.GetExecutingAssembly().CodeBase; UriBuilder uri = new UriBuilder(codeBase); string path = Uri.UnescapeDataString(uri.Path); return Path.GetDirectoryName(path); } Get title of executing assembly: /// <summary> /// Gets the assembly title. /// </summary> public static string GetAssemblyTitle() { object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false); if (attributes.Length > 0) {…

Using a Comparer to Sort CultureInfos by their Name

If you need to show a list of languages, or more general CultureInfos, you might want to sort them by the name they show up in the GUI. This can easily be done by using a custom comparer. This implements the generic IComparer<T> interface. public class CultureInfoComparer : IComparer<CultureInfo> { public int Compare(CultureInfo x, CultureInfo…

Calendar tools and algorithms

Calculating a leap year The correct calculation of a leap year was first introduced by pope Gregor in 1582. The Gregorian Calendar is still commonly used in most parts of the world. Before that, the leap years were simply defined being every 4th year. public static bool IsLeapYear(int year) { if (year % 100 ==…

Check Availability of a Server

This checks the availability of a server by sending a ping to the given IP address and waiting a certain amount of time for a response from the server: public static bool ServerAvailable(string ip, int timeout) { Ping pingSender = new Ping(); PingReply reply; try { reply = pingSender.Send(ip, timeout); } catch { return false;…