Thursday, June 24, 2010

Code: WPF Binding a TreeView to an Array of Complex Objects

I’m taking my first stab at WPF as a learning exercise and figured out how to bind an array of objects (with sub-objects) to a TreeView control. 

First, here is the resulting output:

image

 

This TreeView binds to an Array of type ClientWithAssets, which contains an array of Asset and a Client as properties. 

image

Step 1: make sure your sub-objects are public properties and not members! (I was stuck for an hour because of this).

Step 2: Drop a TreeView control on your window and define the hierarchy:

        <TreeView HorizontalAlignment="Stretch" Margin="12,41,12,12" Name="treeView1" VerticalAlignment="Stretch" ItemsSource="{Binding}" >
<
TreeView.ItemTemplate>
<
HierarchicalDataTemplate ItemsSource="{Binding Assets}">
<
TextBlock Text="{Binding ClientItem.ClientName}"/>
<
HierarchicalDataTemplate.ItemTemplate>
<
DataTemplate>
<
TextBlock Text="{Binding Name}"/>
</
DataTemplate>
</
HierarchicalDataTemplate.ItemTemplate>
</
HierarchicalDataTemplate>
</
TreeView.ItemTemplate>
</
TreeView>


Step 3: Bind on window load:



private void Window_Loaded(object sender, RoutedEventArgs e)
{
treeView1.ItemsSource = ClientWithAssets.FindAll();
}

There are many, many ways to perform this same functionality, but this is what made most sense to me.

Thursday, June 17, 2010

Code: Create Properties for ASP.Net Web Form Pages

When developing web form pages I like to create properties to interact with user input controls that are more complex than simple strings (Enums, DateTime, etc).  This allows me to keep more of my page strongly typed.

using System;

namespace WebFormsSamples
{
public partial class PageProperties : System.Web.UI.Page
{
public enum PrimaryColors
{
Red,
Blue,
Yellow
}

public PrimaryColors SelectedColor
{
get
{
PrimaryColors result;
Enum.TryParse<PrimaryColors>(ColorInput.SelectedValue, out result);
return result;
}
set
{
ColorInput.SelectedValue = value.ToString();
}
}

public DateTime SelectedDate
{
get
{
DateTime result = DateTime.MinValue;
DateTime.TryParse(DateInput.Text, out result);
return result;
}
set
{
DateInput.Text = value.ToShortDateString();
}
}

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindControls();
SelectedColor = PrimaryColors.Yellow;
SelectedDate = DateTime.Today;
}
}

/// <summary>
///
Notice the strong types here!
/// </summary>
protected void SaveInput_Click(object sender, EventArgs e)
{
SaveItem(SelectedColor, SelectedDate);
}

private void BindControls()
{
ColorInput.DataSource = Enum.GetNames(typeof(PrimaryColors));
ColorInput.DataBind();
}

private void SaveItem(PrimaryColors color, DateTime date)
{
lblResult.Text = SelectedColor.ToString() + " " + date.ToLongDateString();
}
}
}

Tuesday, June 8, 2010

Code: Encoding Videos using Expression Encoder

I wrote an article for MSDN magazine about Expression Encoder, check it out at http://msdn.microsoft.com/en-us/magazine/ff714558.aspx. This is my first article pretty exciting!

Wednesday, June 2, 2010

Code: C# Optional Parameters

One of the new features of C# in .Net 4.0 is optional parameters.  VB has had this language feature for a while, and its nice to see it make its way to C#.  If you don’t like optional parameters you can still use method overloading, but it will definitely be useful in some situations.  To make a parameter optional, you just need to specify what the default value for the parameter is:

public int Add(int firstNumber, int secondNumber = 0, bool isAbsolute = false)
{
int result = firstNumber + secondNumber;

if (isAbsolute)
result = Math.Abs(result);

return result;
}

add