|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
IntroductionSilverlight 2 is a great idea, but there are some gaping holes. One of the things I missed was support for ordered and unordered lists - a counterpart for the HTML <ol> and <ul> tags. This library fills that gap.
Requirements
Installation
Add OrderedList elements to the toolbox
The new controls are only visible when you have a .xaml file open. You won't see them while looking at an .aspx file. Quick StartAdd references to the library When you drag an OrderedList element from the toolbox to your page, the editor automatically adds the required namespace/assembly declaration to your page and adds a reference to OrderedList.dll to your project. However, if you simply type in the XAML, or copy and paste, be sure to to go through these steps to add these things yourself:
Simple list To add an ordered list on your page, simply add something like the following to the XAML: <my:OrderedList> <StackPanel> <my:ListItem> <TextBlock>First text item</TextBlock> </my:ListItem> <my:ListItem> <TextBlock>Second text item</TextBlock> </my:ListItem> <my:ListItem> <TextBlock>Third text item</TextBlock> </my:ListItem> </StackPanel> </my:OrderedList> The
Multiple elements per item
<my:ListItem> <StackPanel> <TextBlock>First text item, first text block</TextBlock> <TextBlock>First text item, second text block</TextBlock> <TextBlock>First text item, third text block</TextBlock> </StackPanel> </my:ListItem> Nested Lists Finally, you can place any elements within a <my:ListItem> <StackPanel> <TextBlock>First text item</TextBlock> <my:OrderedList> <StackPanel> <my:ListItem> <TextBlock>First nested text item</TextBlock> </my:ListItem> <my:ListItem> <TextBlock>Second nested text item</TextBlock> </my:ListItem> <my:ListItem> <TextBlock>Third nested text item</TextBlock> </my:ListItem> </StackPanel> </my:OrderedList> </StackPanel> </my:ListItem> Putting it all together Below is a page with the completed example. You'll find it in the sources you downloaded, in project Example1.<UserControl x:Class="Example1.Page" xmlns:my="clr-namespace:OrderedList;assembly=OrderedList" xmlns="http://schemas.microsoft.com/client/2007" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="400" Height="300"> <Grid x:Name="LayoutRoot" Background="White"> <my:OrderedList> <StackPanel> <my:ListItem> <StackPanel> <TextBlock>First text item</TextBlock> <my:OrderedList> <StackPanel> <my:ListItem> <TextBlock>First nested text item</TextBlock> </my:ListItem> <my:ListItem> <TextBlock>Second nested text item</TextBlock> </my:ListItem> <my:ListItem> <TextBlock>Third nested text item</TextBlock> </my:ListItem> </StackPanel> </my:OrderedList> </StackPanel> </my:ListItem> <my:ListItem> <StackPanel> <TextBlock>Second text item, first text block</TextBlock> <TextBlock>Second text item, second text block</TextBlock> <TextBlock>Second text item, third text block</TextBlock> </StackPanel> </my:ListItem> <my:ListItem> <TextBlock>Third text item</TextBlock> </my:ListItem> </StackPanel> </my:OrderedList> </Grid> </UserControl> DatatemplateYou can use a <my:OrderedList> <ListBox x:Name="MyBooks" ItemsSource="{Binding Mode=OneWay}"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal" > <my:ListNumber/> <TextBlock Text="{Binding Title}" Width="150" Margin="0,0,5,0" /> <TextBlock Text="{Binding ISBN}" Width="120" Margin="0,0,5,0" /> <TextBlock Text="{Binding PublishDate}" Width="200" Margin="0,0,5,0" /> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </my:OrderedList> Note that:
The difference between a The project Example2 in the solution you downloaded has a working example of all this. Number TypesIn addition to decimal numbers you can use upper- and lowercase letters and upper- and lowercase roman numerals. You specify the number type via the For example, to use lowercase roman numerals, you'd write: <my:OrderedList NumberType="LowerRoman"> ..... </my:OrderedList>
BulletsTo make it easy to have bullets instead of numbers, the For example, to use a black square as your bullet, you'd write: <my:OrderedList NumberType="Square"> ..... </my:OrderedList>
More bullets If the bullet you want isn't listed above, you can use any Unicode character you want by using the For example, to use a smiley as your bullet, you'd write: <my:OrderedList NumberFormat="☺"> ..... </my:OrderedList> You'll find lots of interesting characters in the Unicode Geometric Shapes Block and the Miscellaneous Symbols Block. Using an image as a bullet Say you have an image Here is how you would use that image as your bullet: <my:OrderedList NumberType="Image" ImageHeight="20" ImageWidth="20" ImageSource="images/greentick.png"> ..... </my:OrderedList> Note that you need to set NumberType to For this to work, your image needs to be present in the proper directory while the page with your Silverlight is being displayed. To ensure this happens:
Also, Silverlight doesn't seem to support .gif files, so you can only use .jpg and .png files. The Example3 project has working examples of all the bullets. Suppressing the bullet If for some reason you don't want a bullet (or number) at all, you'd write: <my:OrderedList NumberType="None"> ..... </my:OrderedList> This however also suppresses the indent. If you do want the indent (but no bullet or number), just use an empty string as your bullet: <my:OrderedList NumberFormat=""> ..... </my:OrderedList> Parent NumbersWith nested lists, sometimes there is a need to show the numbers of the "parent" items in the "child" items, like this: To achieve this effect, use the <my:OrderedList ShowParentNumbers="True"> ..... </my:OrderedList> A working example is in the Example4 project. You can have as many nesting levels as you want. Start and IncrementWhen using numbers, you don't have to start at 1 and increment by 1. If you wanted to start at 10 and increment by 5, simply write: <my:OrderedList Start="10" Increment="5"> ..... </my:OrderedList> Adding Fixed TextSuppose you want to embellish the item numbers with some text, like this: 1) ..... or even this: Step 1: ..... This is easy to achieve with the <my:OrderedList NumberFormat="{}Step {0}:"> ..... </my:OrderedList>
If you want just fixed text (without a number), leave out both the Spacing and AligningThe position of the number or bullet, and the indentation of the text, are determined by these properties:
If you don't set any of these properties, you get a default NumberWidth that's big enough for a few digits, a "right" NumberTextAlignment and a small NumberPostdent so the text doesn't hug the number. In most cases, this is what you want. Sometimes you'll want to set the width yourself, for example to make it big enough for fixed text: <my:OrderedList NumberIndent="10" NumberWidth="40" NumberPostdent="5" NumberTextAlignment="Left" NumberFormat="{}Step {0}:"> ..... </my:OrderedList> Example5 has a working example of this. Spacing for image bullets Things are slightly different when using image bullets. Here, the property ImageWidth is used instead of NumberWidth, and NumberTextAlignment is ignored:
Number AppearanceHow about red bullets? Or really big underlined numbers? If you have a look at the style for ListNumber towards the end of the generic.xaml file in the OrderedList project, you'll see that the number or bullet sits in a TextBlock element. You can set the most important properties of the TextBlocks holding the numbers or bullets via properties of the OrderedList element. The property names are the same as those for the corresponding TextBlock properties, except they have "Number" prepended. So you can write something like this: <my:OrderedList NumberForeground="Red" NumberFontFamily="Arial" NumberFontSize="24"> ..... </my:OrderedList> You can use these properties:
The Test project within the solution shows the use of some of these properties. History
9 June 2008: First release, for Silverlight 2 Beta 1
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||