Zum Hauptinhalt springen

Obtain IP and MAC Address in C#

There is no built-in way to discover the MAC address of your network adapter in .NET. Using the following function the MAC address that belongs to any IP address can be obtained. The functions use the external DLL iphlpapi.dll. public static class Network { [DllImport(„iphlpapi.dll“, ExactSpelling = true)] private static extern int SendARP(int DestIP, int […]

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) […]

WPF Fix: Style is Only Applied to First ...

There is a bug in WPF that leads to a very strange behavior: A style that is defined in the resources is not applied to the first element, but to all others. This only happens when MergedDictionaries are cascaded. The following code is a valid XAML definition, but will not work as expected: <Application.Resources> <ResourceDictionary> […]

Automatically grayed Image in WPF

The following class is a WPF image control which is directly derived from Image. Therefore it can be used out-of-the-box like the standard WPF Image control. The image will automatically change to a gray scale image when the IsEnabled property of the control is set to false. The AutoGrayableImage can be used in within any […]

Showing a ComboBox with Different Line T...

This can be used to choose the line type from a ComboBox, for example in a drawing application. The first entry is a solid line, the second is a dashed line and the last entry displays a dotted line. <ComboBox Width=“80″ SelectedIndex=“0″> <ComboBoxItem Height=“23″> <Line X1=“0″ X2=“60″ Y1=“11″ Y2=“11″ Stroke=“Black“ StrokeThickness=“2″ /> </ComboBoxItem> <ComboBoxItem Height=“23″> […]

Text Drop Shadow Effect in XAML

A DropShadowEffect can be used to make any WPF control cast a shadow. In order to use this on a text, we can simply assign the effect to the Effect property of the TextBlock. DropShadowEffect contains properties to set the shadow depth, the direction, color of the shadow, opacity and a blur radius. This gives […]

Define Enums as ItemsSource in WPF

Enumerations are often used in applications to enable the user to select an item from a list of predefined values. This can be handled in the ViewModel by publishing a list of the enum values. But there is also another way that uses a XAML only approach. Example: public enum SampleEnum { Dog, Cat, Bird […]

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 […]

Adding Localization to .NET WPF Applicat...

Here I’d like to describe some approaches how .NET applications can be localized. General In general the texts that are supposed to be localized should be added to the Resources.resx file located under Properties of the project: The data is stores in an XML format, but also type save access properties are created automatically. Don’t […]