Tag Archive: Silverlight


In the past, many times I have come around a situation where I want to load a completely different set of facets in the Pivot Viewer without really instantiating a new instance of it! While in the Pivot Viewer’s first version it was relatively simpler (by just loading a different cxml) but in the second version it didn’t work right, when I was using the client-side loading of items. There was always something that didn’t work example the filter pane wouldn’t show the facets of the new collection, etc. But, there’s a very simple solution for this (actually it was too simple to be believable for the first time…at least for me), so here it is…before you set the new PivotProperties and the Templates, just write down the following 3 lines:

ItemPivot.ItemsSource = null;
ItemPivot.PivotProperties.Clear();
ItemPivot.ItemTemplates.Clear();

Thats it! And you’ll never face a problem in loading a new set of items following a different structure again! Kapish? 🙂

Advertisements

In Silverlight 5 (and may be earlier), you would face a real frustrating issue when using ChildWindows…the problem is as soon as you close a ChildWindow the application would freeze. Well, the solution is pretty simple it requires you to copy-paste the following piece of code to your ChildWindow’s class:

protected override void OnClosed(EventArgs e)
{
   base.OnClosed(e);
   Application.Current.RootVisual.SetValue(IsEnabledProperty, true);
}

Code Source: Silverlight Community

While I am not the guy who found it, but I sure have used it a lot of times and trust me, it does work.

Cheers! 🙂

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!

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!

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

I have been working around with the Silverlight Pivot Viewer for long time now and I have noticed that whenever I use a JIT collection I dont see Description in the facet panel but when I use a static collection, Description is available. First I thought maybe description facet is not allowed in JIT collections but then I remembered that the Collection class constructor does ask for Description in its parameter, this made my curious and I started dive into the code of PivotServerTools Project and I found that there’s no place in the code where this Description is used apart from when creating the default item images. After a lot of searching I was able to find a solution using which Description can be made visible. To do so, follow the below steps:

1. Go to the PivotServerTools project.
2. Open the Internal folder.
3. There you’ll see a “CxmlSerializer” class, this class is used when the method Collection.ToCXML() is invoked, which is done by the Pivot Viewer. Open this class and navigate to the method “MakeItemContent”. In this method add the following line:

if (!string.IsNullOrEmpty(item.Description)){
	yield return new XStreamingElement(Xmlns + "Description", item.Description);
}

This way Description will start showing up in the Facet panel of every collection provided you specified a value for it in the Item’s collection class constructor.