In this tutorial, we will create the standard Silverlight ListBox will be customized to be functionally similar to a ListBox you would find on a Mac.
The XAML for this tutorial contains a custom style that we use to disable the scrollbar:
<UserControl x:Class="CustomListBox.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
<Grid x:Name="LayoutRoot" Background="White">
<Grid.Resources>
<Style x:Key="ListBoxStyle1" TargetType="ListBox">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBox">
<Grid x:Name="LayoutRoot">
<Border Padding="5" BorderBrush="#000000" BorderThickness="1" Background="#ffffff" CornerRadius="0">
<ScrollViewer x:Name="ScrollViewer" VerticalScrollBarVisibility="Hidden" Padding="{TemplateBinding Padding}" Background="{TemplateBinding Background}" BorderBrush="Transparent" BorderThickness="0">
<ItemsPresenter />
</ScrollViewer>
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>
<StackPanel Margin="4" HorizontalAlignment="Left">
<RepeatButton Width="200" Height="22" Click="Up_Click">
<Polygon Points="5,0 10,10 0,10 5,0" Fill="#222222" />
</RepeatButton>
<ListBox x:Name="listbox" Width="200" Height="150" Style="{StaticResource ListBoxStyle1}">
<ListBoxItem Content="Item 1" />
<ListBoxItem Content="Item 2" />
<ListBoxItem Content="Item 3" />
<ListBoxItem Content="Item 4" />
<ListBoxItem Content="Item 5" />
<ListBoxItem Content="Item 6" />
<ListBoxItem Content="Item 7" />
<ListBoxItem Content="Item 8" />
<ListBoxItem Content="Item 9" />
<ListBoxItem Content="Item 10" />
<ListBoxItem Content="Item 11" />
<ListBoxItem Content="Item 12" />
</ListBox>
<RepeatButton Width="200" Height="22" Click="Down_Click">
<Polygon Points="5,10 10,0 0,0 5,10" Fill="#222222" />
</RepeatButton>
</StackPanel>
</Grid>
</UserControl>
In XAML, just apply the custom style and populate it with some test data. There are also two repeat buttons, an up and down that will handle the scrolling for us:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace CustomListBox
{
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
}
private void Up_Click(object sender, RoutedEventArgs e)
{
if (listbox.Items.Count > 0)
{
int newIndex = listbox.SelectedIndex - 1;
if (newIndex < 0)
{
newIndex = 0;
}
listbox.SelectedIndex = newIndex;
}
}
private void Down_Click(object sender, RoutedEventArgs e)
{
if (listbox.Items.Count > 1)
{
int newIndex = listbox.SelectedIndex + 1;
if (newIndex >= listbox.Items.Count)
{
newIndex = listbox.Items.Count - 1;
}
listbox.SelectedIndex = newIndex;
}
}
}
}
And now we're done!
HostForLIFE.eu Silverlight 6 Hosting
HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes. We have customers from around the globe, spread across every continent. We serve the hosting needs of the business and professional, government and nonprofit, entertainment and personal use market segments.