Archive

Archive for the ‘.NET’ Category

Useful WPF Data Converters

May 31st, 2012 No comments

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:

  1. Define the XAML namespace to point to the common namespace where you would store your common converters. For example:
  2. <window ...
           xmlns:converters="clr-namespace:MyCompany.MyProject.MyConverters" >
  3. Use converter like this:
  4. <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…

Several Facts about C# Events and Delegates

September 10th, 2010 No comments

Here I put some facts about events and delegates.

Event vs. Delegate

  • “event” in delegate definition is just a limitation of delegate invocation. If you use “event” you can invoke the delegate from the scope of the class ONLY. Usually you would not like a client of your class to directly trigger an event. Use “event” to encapsulate your delegates.
  • Interfaces define public methods and properties of the class. Interface does not define member variables. In order to specify a delegate in an interface an “event” modifier should be used.
  • “event” also provides an ability to perform some actions when something is subscribed or unsubscribed from the delegate. For that purpose “add” and “remove” accessors should be used inside the definition of an event (just like get/set accessors in a regular property).

Duplicate Subscription

  • Duplicate subscription to events results in duplicate invocation of subscribed method.

“MyEvent += Function” vs. “MyEvent += new Handler(Function)”

  • This syntax produces the same result. In both cases a “Function” is subscribed to “MyEvent”.

“MyEvent -= Function” vs. “MyEvent -= new Handler(Function)”

  • This syntax produces the same result. In both cases a “Function” is unsubscribed from “MyEvent”.


Identifying and Eliminating C#/WPF Memory Leaks

June 30th, 2010 No comments

PhotoSuck Beta is now released!

October 21st, 2009 2 comments

Hi everybody. At last long-awaited “PhotoSuck Beta” is on air. Its still in testing stage and You’re welcome to be one of its first users. For those guys who are new to “PhotoSuck” – its a windows based application which downloads Your desired photos from Flickr and Picasa. Your desires are defined by sets of criterias. Criterias include social relevance(specific users’ photos, Your friends’ photos, …); tag relevance; picture quality (portrait, landscape, HD, SD, …); upload date; and more. All this is achieved using cute WPF application, which looks like this:

PhotoSuck Beta Main Screen

Read more…

Attached Files:

C# Object To Interface Caster Utility

April 23rd, 2009 9 comments

Introduction

Here I describe a unique way of casting any object to an interface that it actually exposes, even though it does not formally implement the particular interface. In order to get the idea what I’m trying to propose here, consider examples below.

Lets say you’re utilizing 3rd party libraries(LibA and LibB) that are dealing with persons. These libraries have PersonLibA and PersonLibB classes defined and are exposing instances of these persons. Most likely they would expose similar public interface and would have properties like Name, EMail, Address, DateOfBirth, etc.  I’m almost sure that you would like to access these persons uniformly. If you had the source code of these components, you could define a common interface and implement PersonLibA and PersonLibB classes from your newly defined interface. However, if the source code is not available that would not work out. The only thing left to do is to define wrapper classes for each of person classes and implement the common interface.
Read more…

Attached Files:

WPF Animatable Button

April 10th, 2009 2 comments

Introduction

This article is about simplifying the process of defining animation for button mouse over, pressed, enabled and disabled animations. The process of defining triggers is quite complicated when you include all IsMouseOver, IsPressed and IsEnabled states. You will also need to work hard and define BeginStoryboard/StopStoryboard precisely to make your desired animation start. Some of these states are overlapping. For instance when the button is in IsPressed state, most likely it will be also in IsMouseOver state and what leads to improper animation to act. It is also quite annoying to define all these complex triggers every time for such a common problem.
Read more…

Attached Files:

C# Object Cloning Machinery

March 16th, 2009 3 comments

Introduction

In most cases, no matter what area of software development you are involved in, you will need a functionality of cloning your objects. The .NET Framework provides you 3 ways of achieving your object copying goal:

  1. Implement the ICloneable interface, and develop cloning functionality manually for each class involved in your desired cloning.
  2. Serialize you object to binary stream and deserialize it utilizing BinaryFormatter and MemoryStream classes. In this case your classes will need to be Serializable.
  3. Clone using Reflections.

Read more…

Attached Files:

WPF Maskable TextBox for Numeric Values

March 10th, 2009 39 comments

Introduction

This article describes how to enhance WPF TextBox and make it accept numeric(integer and floating point) values. The second goal is make the TextBox smart enough to have it easier to input numerics. The easy means to provide the TextBox with some kind of intelligence not just rejecting non-numeric symbols. Provided extension also allows setting minimum and/or maximum values.
Read more…

Attached Files:

C# Coding Standard, Naming Conventions and Practices

March 1st, 2009 5 comments

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…