Conversion between Pixels and PixelLength

In this help article I will demonstrate how you could set real pixels to the PixelLength property of the RadGanttView control which is of TimeSpan type.

Usually you would be setting the PixelLength property to for example 1 hour and 30 minutes but in some cases it is more useful to be able to set the PixelLength to real pixels in order to be able to fill a specific area in your application. In those cases you could either test and see what TimeSpan is appropriate for you or use the approach that is discussed in this article to set it to exact pixel number.

Converting Pixels to PixelLength

The PixelLength property of the RadGanttView control is responsible for setting the horizontal size of the TimeRuler and in order to set it to a value that represents pixels you will need to implement the following formula:

PixelLength = (Ticks of the VisibleRange) / (desired pixels)

The next code snippets shows how to implement the above formula in order to set the PixelLength to 760 pixels:

<telerik:RadGanttView x:Name="GanttView"> 

var maxTicks = this.GanttView.VisibleRange.End.Subtract(this.GanttView.VisibleRange.Start).Ticks / 760; 
this.GanttView.PixelLength = TimeSpan.FromTicks(maxTicks); 

Converting PixelLength to Pixels

The inverse conversion of PixelLength to pixels could be implemented by the following formula:

Pixels = (Ticks of the VisibleRange) / (desired PixelLength.Ticks)

The next code snippets show how to implement the described above formula in order to get the Pixels representation of desired TimeSpan:

<telerik:RadGanttView x:Name="GanttView"> 

var maxTicks = this.GanttView.PixelLength.Ticks; 
var pixels = this.GanttView.VisibleRange.End.Subtract(this.GanttView.VisibleRange.Start).Ticks / maxTicks; 

Find a runnable project of the previous example in the WPF Samples GitHub repository.

In this article
Not finding the help you need? Improve this article