To download the Windows 8 version click here
For the Windows Phone version click here







using System;
using System.Collections.ObjectModel;
namespace HorizontalBarChartExample
{
public class ElectionResult
{
public String Party { get; set; }
public double Percentage { get; set; }
public int Votes { get; set; }
}
public class ElectionResults : ObservableCollection
{
public ElectionResults()
{
Add(new ElectionResult { Party = "Conservative", Percentage = 36.1, Votes = 10703754 });
Add(new ElectionResult { Party = "Labour", Percentage = 29.0, Votes = 8609527 });
Add(new ElectionResult{ Party="Liberal Democrat", Percentage=23.0, Votes=6836824 });
Add(new ElectionResult { Party = "Other", Percentage = 11.9, Votes = 3532874 });
}
}
}
using System;
using System.Globalization;
using System.Windows.Data;
namespace HorizontalBarChartExample
{
public class PartyColourConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is String)
{
var party = (String)value;
switch (party)
{
case "Labour":
return "Red";
case "Conservative":
return "Blue";
case "Liberal Democrat":
return "Orange";
default:
return "Gray";
}
}
return "";
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
using System;
using System.Globalization;
using System.Windows.Data;
namespace HorizontalBarChartExample
{
public class PercentageConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is double)
{
return String.Format("{0}%", value);
}
return "";
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
using System;
using System.Globalization;
using System.Windows.Data;
namespace HorizontalBarChartExample
{
public class VotesConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is int)
{
return String.Format("({0:##,#})", value);
}
return "";
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
using System;
using System.Globalization;
using System.Windows.Data;
namespace HorizontalBarChartExample
{
public class BarWidthConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is double)
{
double scaled = (double)value * 6.5;
return scaled.ToString();
}
return "";
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
<Application.Resources>
<converter:PercentageConverter x:Key="PercentageConverter" />
<converter:BarWidthConverter x:Key="BarWidthConverter" />
<converter:VotesConverter x:Key="VotesConverter" />
<converter:PartyColourConverter x:Key="PartyColourConverter" />
</Application.Resources>
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.Resources>
<src:ElectionResults x:Key="electionResults"/>
</Grid.Resources>
<ListBox ItemsSource="{StaticResource electionResults}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Party}" />
<StackPanel Orientation="Horizontal">
<Rectangle Fill="{Binding Party, Converter={StaticResource PartyColourConverter}}"
Width="{Binding Percentage, Converter={StaticResource BarWidthConverter}}" Height="30" />
<TextBlock Margin="5,0,5,0" Text="{Binding Percentage, Converter={StaticResource PercentageConverter}}" />
<TextBlock Text="{Binding Votes, Converter={StaticResource VotesConverter}}" />
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle" Text="UK Election Results" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="2010" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<Grid x:Name="ContentPanel" Grid.Row="1">
<my:HorizontalBarChartExampleControl Margin="25,0,0,0" />
</Grid>
</Grid>