Zum Hauptinhalt springen

Using Enums in ASP.NET MVC

If you use enumerations, you usually prefer not to see the internal value of the enum in the GUI, but a human readable (maybe localized) string. The following example shows how easy it is to accomplish that in ASP.NET MVC. We use the following example class that represents a change in a software: public class […]

How to Implement a DependencyPropertyWat...

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

Creating ComboBox with System Colors in ...

This snippet shows how to display a list of system colors in XAML only. First we create ab object data source containing the list of colors: <ObjectDataProvider ObjectInstance=“{x:Type Colors}“ MethodName=“GetProperties“ x:Key=“ListOfColors“ /> This data source obtains the list of colors from the properties of the Colors type. In the next step we define a data […]

Workaround for the „File“ Bug in the WPF

This post shows how to overcome an annoying bug in the Microsoft RibbonControlsLibrary. When you define the ribbon and change the label of the „File“ menu, the changed text is not shown, instead you’ll always see the „File“ text. Especially when you want to define your own localized strings, this can be a show stopper. […]

Data Binding to Visibility Property

The following describes how to used a converter to directly bind your ViewModel properties to the Visibility of controls in your view. First we define a converter that converts a boolean value into a Visibility enum value and back. [ValueConversion(typeof(bool), typeof(Visibility))] public class VisibilityConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, […]

ViewModel Validation in MVVM

In WPF IValidatableObject, IDataErrorInfo interface implementations can be used to validate user input against validation rules. This blow shows a different approach that defines a IValidatingViewModel as interface for ViewModels that should use a validation. This is a very simple approach without relying on some black box third party technology. Validation Interface This is the […]

Loading Files into a WPF FlowDocument

Das WPF FlowDocument ist eine einfache Möglichkeit, um Fließtext in WPF darzustellen. Hier zeige ich Methoden, um Textdateien in ein FlowDocument einzulesen. XAML: <FlowDocumentScrollViewer x:Name=“documentViewer“ /> Loading of a text file: string fileName = @“Lizenz.txt“; if (File.Exists(fileName)) { var run = new Run(File.ReadAllText(fileName)); var p = new Paragraph(run); var flowDocument = new FlowDocument(p); documentViewer.Document = […]

Using Data Annotations for Validation in...

The .NET Framework uses many different approaches for validation. The ASP.NET MVC for example uses Data Annotations to annotate the model classes with the definition of valid ranges, etc. In WPF on the other hand, validation rules are used to validate entries made by the user and display error messages. This post shows how to […]

Convert .NET DateTime into Unix Time

The Unix calendar starts at 01/01/1970. This post will show you how to convert any DateTime value into a Unix time stamp. private const string InvalidUnixEpochErrorMessage = „Unix time starts January 1st, 1970“; /// <summary> /// Convert a long into a DateTime /// </summary> public static DateTime FromUnixTime(this Int64 self) { var ret = new […]

Load Multiple Levels in Entity Framework...

When you use the Entity Framework to query data from a database, you often have to load detail data related to a database entry. Child elements can easily be obtained from the database with LINQ. Loading direct children: var steps = from b in db.Steps.Include(s => s.Questions) orderby b.StepOrder select b; By using the include […]