Just sharing the most common WPF data binding converters that I encounter on day to day basis. This should be beneficial for every WPF developer.
Usage
Usage in XAML should be as follows:
- Define the XAML namespace to point to the common namespace where you would store your common converters. For example:
<window ...
xmlns:converters="clr-namespace:MyCompany.MyProject.MyConverters" >
- Use converter like this:
<button Content="Push Me" Click="ButtonConvertToBool_Click"
Visibility="{Binding Path=Something, Converter={x:Static converters:BoolToVisibleConverter.Instance}}"
IsEnabled="{Binding Path=SomeOtherThing, Converter={x:Static converters:IsNotNullConverter.Instance}}"></button>
Common Converters
IsNullConverter
Gets an object as an input and returns true if the object is not null; otherwise returns false.
public class IsNullConverter
: IValueConverter
{
private static readonly IsNullConverter _instance
= new IsNullConverter
();
public static IsNullConverter Instance
{
get
{ return IsNullConverter
._instance
; }
}
private IsNullConverter
()
{
}
public object Convert
(object value, Type targetType,
object parameter,
System.Globalization.CultureInfo culture
)
{
try
{
return (value
== null);
}
catch
{
return null;
}
}
public object ConvertBack
(object value, Type targetType,
object parameter,
System.Globalization.CultureInfo culture
)
{
return Binding
.DoNothing;
}
}
Read more…
Attached are some code snippets that I use during code development.
- newClass – code Snippet for creating a class
- newClassNotify – code Snippet for creating a class which implements INotifyPropertyChanged interface
- dprop – code snippet for a property using DependencyProperty
- dpropu – code snippet for a property using DependencyProperty with ChangeCallback notifier
- dpropObCollection – code snippet for a property using DependencyProperty to an ObservableCollection
- conv – code Snippet for creating a IValueConverter
- multiconv – code Snippet for creating a IMultiValueConverter
- propNotify – code Snippet for creating a property with get/set accessors and property value change notification trigger
Today I encountered another wild animal while surfing in the jungle. Pretty much nothing to comment 🙂
if (visiblePath
== null)
{
visiblePath
= new DispatcherTimer
();
visiblePath
.Tick -= new EventHandler
(visiblePath_Tick
);
visiblePath
.Tick += new EventHandler
(visiblePath_Tick
);
visiblePath
.Interval = TimeSpan
.FromSeconds(500);
}
else
{
visiblePath
.Tick -= new EventHandler
(visiblePath_Tick
);
visiblePath
.Tick += new EventHandler
(visiblePath_Tick
);
}
visiblePath
.Stop();
visiblePath
.Start();
Today at the office I encountered another nightmare. It seems like the author of the code above was doing the development late at night, he sleep in the middle and unintentionally pushed Ctrl-V button.

Read more…
Today at the workplace I came across a very weird chunk of code. An enormous block of code was wrapped in 25-30 levels of brackets. On a a standard resolution screen (1280×1024) the code was completely out of screen. After spending some time on refactoring it and eliminating annoying scopes I decided to take a screenshot and post it here.

Below I will describe reasons that can lead to such kind of issues and possible solutions.
Read more…
Here I describe coding style and conventions you would encounter when going over my source codes. Most of them are easy to follow and understand. Read more…