Tag Archive: silverlight-5


Pivot Viewer v2: Custom actions on items

A good post here by “Good Coffee Good Code” guy explains the complete process and provides a sample solution here. Download and check it out!

Advertisements

In Pivot Viewer v2, sorting of multivalued facets is not taken care of automatically (i.e. if you are binding items in the non-cxml way). I took the sample provided at the end Pivot Viewer’s documentation here and changed the Employee class to:

public class Employee
{
	public Employee(string firstname, string lastname, DateTime? birthDate, string department, string office, string title, string officePhone, string personalPhone)
	{
		FirstName = firstname;
		LastName = lastname;
		Birthdate = birthDate;
		Department = new List<string> { department, "D1", "D2", "D3", "D4", "D5" };
		Office = office;
		Title = title;
		OfficePhone = officePhone;
		PersonalPhone = personalPhone;
	}

	public string FirstName { get; set; }
	public string LastName { get; set; }
	public DateTime? Birthdate { get; set; }
	public List<string> Department { get; set; }
	public string Office { get; set; }
	public string Title { get; set; }
	public string OfficePhone { get; set; }
	public string PersonalPhone { get; set; }
}

i.e. the “Department” variable is now of List<string> type. Then, I added “Department” as a Filterable Pivot Property in the xaml like:

<sdk:PivotViewerStringProperty Id="Department" Options="CanFilter" DisplayName="Department" Binding="{Binding Department}" />

The result was when I tried to sort on this property, it threw an error:

“Failed to compare two elements in an array. At least one object must implement IComparable”

As the error explains, the error comes because the List object doesn’t have an implementation for IComparable due to which the sorting is not supported. It kind of makes sense too since a generic sorting criteria of how a List object is compared with another cant be decided. To make this work, I created a custom class that inherits the List object and has an implementation for the IComparable interface. For the sorting comparison, I did it according to the number of items in an object compared with the other. I know its not going to be correct in most of cases, but hey…its just a feasibility shown here, you can sophisticate the sorting mechanism as much as you like! 🙂 The custom class goes like:

/// <summary>
/// Custom class to support multivalued facets sorting
/// </summary>
public class StringFacets : List<string>, IComparable
{
	#region Implementation of IComparable

	/// <summary>
	/// Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.
	/// </summary>
	/// <returns>
	/// A 32-bit signed integer that indicates the relative order of the objects being compared. The return value has these meanings: Value Meaning Less than zero This instance precedes <paramref name="obj"/> in the sort order. Zero This instance occurs in the same position in the sort order as <paramref name="obj"/>.Greater than zero This instance follows <paramref name="obj"/> in the sort order.
	/// </returns>
	/// <param name="obj">An object to compare with this instance. </param><exception cref="T:System.ArgumentException"><paramref name="obj"/> is not the same type as this instance. </exception>
	public int CompareTo(object obj)
	{
		//PUT YOUR SORTING LOGIC HERE
		var stringFacet = obj as StringFacets;
		if (stringFacet == null)
			return -1;

		if (stringFacet.Count < this.Count)
			return -1;
		
		if (stringFacet.Count == this.Count)
			return 0;
		
		if (stringFacet.Count > this.Count)
			return 1;

		return -1;
	}

	#endregion
}

 

And the class is to be used as follows in the Employee class:

public StringFacets Department { get; set; }

 

Now, when you will try to sort the items based on the  “Department” category, it would work like a charm. The code is available at https://skydrive.live.com/embedicon.aspx/Blog/SLPV2_Sorting.rar?cid=e24ef727df20622d&sc=documents.

If you would like to share any improvement, its going to be awesome!

Pivot Viewer v2 (released as a part of SL 5 RC) is one of smartest improvements done by MS to its own products. However, there are still somethings which are not very clearly available. For instance, sorting multivalued facets, custom actions, etc. In this series of blogs, I am going to explain to you how one can do the tasks which were quite easy in the old version of Pivot Viewer but not so straight forward in this version and then later I’ll take you through how you can harness the power of this newer version of the super awesome product. The series will evolve as follows:

Stay tuned!

An awesome blog post demonstrating how to harness the power of the new Pivot Viewer (released as a part of SL5 RC).

Joris Dries started a Twitter conversation this morning about getting decent performance out of the PivotViewer in SL5RC. So, I thought now would be a good time to gather a bunch of ideas I've had over the past few months and put them all in one post. This might not answer Joris' question directly, but it does satisfy the title. You can download the complete source code of the examples here. What I'll do is build a number of scenarios and compare … Read More

via Good Coffee Good Code