Creating a Mac Style Silverlight ListBox
In this article the standard Silverlight ListBox will be customized to be functionally similar to a ListBox you would find on a Mac.
You need to login to Download this Custom ListBox example, If you do not have a login you can register for free!
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 our XAML we 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;
}
}
}
}
The two event handlers are all we need here to control the scrolling here, whilst in the XAML the custom style removes the scrollbar.
Your Comments
Post your Comments
Rate this:
1 Star
2 Star
3 Star
4 Star
5 Star
27 Ratings / 3.3 Average