Wednesday, June 22, 2011

WPF - C# Sorting ObservableCollection

After searching the web I found a lot of examples for sorting an ObservableCollection when using custom objects but none with a basic string. So here is what I did:

Having:

ObservableCollection list


Implementing the following method into a static type class:

///
/// Sorts the given list by ascending value.
///

///
public static void BubbleSort(this IList o)
{
for (int i = o.Count - 1; i >= 0; i--)
{
for (int j = 1; j <= i; j++) { object o1 = o[j - 1]; object o2 = o[j]; if (((IComparable)o1).CompareTo(o2) > 0)
{
o.Remove(o1);
o.Insert(j, o1);
}
}
}
}


Calling by using .BubbleSort(list).

The result is the list ordered by ascending value.

Reference: The Sandpit



Wednesday, June 15, 2011

WPF - C# grid element by row and column

I have a table with a column filled with a check box for each row and, on the header row, I have a check box that I want to have the "check all" behaviour.

The check box is within a dock panel so that it can be centred on the table cell even over resize.

To do this I have to go trough all table elements and find out my check boxes to set them checked or not checked.

After some time searching fora solution this is what came out:

public void TopCheckBoxChecked(object sender, RoutedEventArgs e)
{
if (mach != null)
{
int row = 1;
int col = 0;

foreach (UIElement element in m_tableGrid.Children)
{
if (Grid.GetColumn(element) == col && Grid.GetRow(element) == row)
{
(((element as DockPanel).Children[0] as UIElement) as CheckBox).IsChecked = (sender as CheckBox).IsChecked;

row++;
}
}
}
}


Not so simple as I would have hopped.

For reference check this blog entry:

Ian Oakes on WPF and Silverlight

Tuesday, June 14, 2011

WPF - TreeView list update

For updating a tree view (after removing elements) the tree view did not change - all elements still appeared on it.

Solution:

Use an ObservableCollection for the tree view items:

ObservableCollection fileList = new ObservableCollection();


to add:

fileList.Add(file);
list.ItemsSource = fileList;


and remove items like this:

ObservableCollection auxList = new ObservableCollection();

foreach (string item in fileList)
{
if (!TheListview.SelectedItems.Contains(item))
{
auxList.Add(item);
}
}
fileList = auxList;
TheListview.ItemsSource = fileList;


I've tried items refresh, setting the collection to invalid but none worked.

Wednesday, June 08, 2011

C# - Enum to String

The idea was to have an enumerate and use its elements as strings instead of values.

public enum ButtonNames
{
SummaryBtn,
UpdatedBtn,
ToUpdateBtn,
NotInstalledBtn
}


For using the values - an example to write in console:

Console.WriteLine(Enum.GetName(typeof(ButtonNames), ButtonNames.NotInstalledBtn));


Another way

ConsoleWriteLine(ButtonNames.NotInstalledBtn.ToString());


The use in a switch element is not allowed since the enumerates are not constant values... that was my main objective. Will keep trying to find a way.


Hope it helps,


References:

Convert an Enum to a String By Mahesh Chand

WPF - Another C# experience

It has been a while since last wrote anything work related, but it is never too late.

Changed my professional tool to WPF / C# (Visual Studio 2010) but still developing in Action Script and MXML (Flash builder 4.5) therefore this blog will be used (as always) as a reference for articles I consider helpful, comments an tips I pick up along the way.