Schlagwort: WPF

Created with Sketch.

React on events that have been handled before

In WPF there are bubbling Routed Events that travel up the visual tree until they are marked as handled by an event handler. This is for example the click event of a button control. The code behind of this can be for example: The mouse click event is set to handled in the button click…
Weiterlesen

Automatically grayed out Images in WPF – Version 2

This post describes a new, improved versions of the AutoGrayableImage introduced in this blog: https://www.engineeringsolutions.de/wp-admin/post.php?post=334 Why? There were some issues with the original version which made it hard to use in some scenarios. The new version will also include performance improvements and will enable us to use the same approach not only for images, but…
Weiterlesen

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“,…
Weiterlesen

Creating ComboBox with System Colors in XAML

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…
Weiterlesen

Workaround for the „File“ Bug in the WPF RibbonControlsLibrary

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.…
Weiterlesen

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,…
Weiterlesen

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…
Weiterlesen

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 =…
Weiterlesen

Using Data Annotations for Validation in WPF

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…
Weiterlesen

WPF Fix: Style is Only Applied to First Element

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>…
Weiterlesen