Wednesday, October 27, 2010

Rx Lazy Loading Text Box

I started using .Net Reactive (Rx) extensions for work recently.
As a WPF UI developer, one of the nice way of handling a users input is not to do anything until the user have complete his/her action.

In this example; I have a text box, which will auto filter a data grid when the user stop typing in the text box and the text changed event is fired.
This will improve the UI responsiveness, instead of firing after every key is entered.

// Method OnSearchTextChanged is called .5s after Text Box text changed
Observable.FromEvent<TextChangedEventArgs>(textbox, "TextChanged")
.Throttle(TimeSpan.FromSeconds(.5))
.ObserveOnDispatcher()
.Subscribe(OnSearchTextChanged);

This was only possible in the past throught the use of Timers and Thread.Sleep, Rx makes this easier to do.

Lazy Loading Text Box

Please let me know if this helps you.

Sunday, August 15, 2010

WPF Value Type Text Box

I wrote a WPF numeric text box control that can support currency, integer and numeric values.
It can prevent invalid input such as letters, spaces, etc. and can be constrained to a max length value.

For example, with the currency text box; when the text box is in focused the formatting is removed allowing the user to enter the value.
When the text box loses focus, formatting is reapplied and display in the text box.

Value Type Text Box

Please let me know if this helps you.

Wednesday, May 5, 2010

WPF Filter Combo Box

I was asked to write a prototype for a project that I am currently working on.
They wanted to be able to selected for a master combo box and filter the selection of the sub-child combo boxes.

I read an article on stackoverflow, which I base my code sample on.

To do this Master-Child Hierarchy, I have implemented it through the use of the attribute property, by creating a custom attribute (PriorityLevel).

public class Football
{
[PriorityLevel(1)]
public League League { get; set; }

[PriorityLevel(2)]
public string Team { get; set; }
}

I have created the custom attribute with the use of an interface (IAttribute).

public class PriorityLevelAttribute : Attribute, EnumHelper.IAttribute<int>
{
private readonly int _value;

public PriorityLevelAttribute(int value)
{
_value = value;
}

public int Value
{
get { return _value; }
}
}

Another think that I thought would be quite good was to have a strong type attribute for enums.
This extension method will allow you to return a strong type value of your attribute enum value.

/// <summary>
/// Returns the attribute value for a given Enum value.
/// </summary>
public static TR GetAttributeValue<T, TR>(this Enum value)
{
var attributeValue = default(TR);

if (value != null)
{
var fi = value.GetType().GetField(value.ToString());

if (fi != null)
{
var attributes = fi.GetCustomAttributes(typeof(T), false) as T[];

if (attributes != null && attributes.Length > 0)
{
var attribute = attributes[0] as IAttribute<TR>;

if (attribute != null)
{
attributeValue = attribute.Value;
}
}
}
}

return attributeValue;
}

Imagine you have a enum class, and you wanted to have addition information to the enum.
This is a elegant way to implement a strong type value for your enum, throught attribute.

public enum League
{
[DisplayDescription("Argentine Primera Division")]
PrimeraDivision = 1,

[DisplayDescription("English Premier League")]
PremierLeague = 2,

[DisplayDescription("Spanish La Liga")]
LaLiga = 3
}

public class DisplayDescriptionAttribute : Attribute, EnumHelper.IAttribute<string>
{
private readonly string _value;

public DisplayDescriptionAttribute(string value)
{
_value = value;
}

public string Value
{
get { return _value; }
}
}

/// <summary>
/// This is the implementation.
/// </summary>
var displayValue = league.GetAttributeValue<DisplayDescriptionAttribute, string>();

Filter Combo Box

Please let me know if this helps you.

Friday, February 12, 2010

Reading and Writing XML with Linq


In this sample, I am using Linq to construct my objects into XML, then being able to read the XML with Linq and construct it back into my class objects.

Writing (Linq to XML):

var xmlFile = 
new XElement("Movies",
from m in movies
select new XElement("Movie",
new XElement("Title", m.Title),
new XElement("Director", m.Director),
new XElement("ReleaseDate", m.ReleaseDate),
new XElement("Casts",
from c in m.Casts
select new XElement("Cast",
new XElement("Name", c.Name),
new XElement("Dob", c.Dob),
new XElement("Roles",
from r in c.Roles
select new XElement("Role",
new XElement("MovieName", r.Movie),
new XElement("RoleName", r.RoleName)))))));

Reading (XML to Linq):

var movies = 
from m in xmlFile.Descendants("Movie")
select new Movie
{
Title = (string)m.Element("Title"),
Director = (string)m.Element("Director"),
ReleaseDate = (DateTime)m.Element("ReleaseDate"),
Casts = (from c in m.Elements("Casts").Descendants("Cast")
select new Cast
{
Name = (string)c.Element("Name"),
Dob = (DateTime)c.Element("Dob"),
Roles = (from r in c.Elements("Roles").Descendants("Role")
select new Role
{
Movie = (string)r.Element("MovieName"),
RoleName = (string)r.Element("RoleName")
}).ToList()
}).ToList()
};

Reading and Writing Xml Files

Please let me know if this helps you.