Silverlight: Separator within listbox?
I have a listbox with several items in it. I would like to programmatically insert a separator bar into the list box. Is this possible?
MSDN speaks of a Separator开发者_如何学Go control, but Visual Studio doesn't recognize it when I try to instantiate one.
I'm using Silverlight 4.
The Separator control you refer to is in WPF and not supported in Silverlight from what I can see in the documentation.
I would handle this via templates. Set up your template with a representation of the item and a second representation of a separator and then bind the visibility of each to a an IsSeparator flag on your data object.
Then all you need to do is set create a dummy object with the IsSeparator flag set to true to get the new item in your list.
see : http://manfredlange.blogspot.com/2009/04/separator-for-menu-item-in-xaml-wpf.html
I use it in silverlight 4, I simply drag & drop from the toolbox and it added me a reference to the toolkit:
xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit"
and I use it this way:
<toolkit:Separator Margin="15"/>
I did a fix similar to James. Adding a boolean field on to the TableModel that the listbox was being bound.
I had an extra requirement of data grouped by Types, which I left in the LINQ query below. If you don't need this you can use allData.Count in place of numDataTypes. My boolean was called IsLastItem and was populated with:
int numDataTypes = allData.Select(o => o.Type).Distinct().Count();
IEnumerable<TableModel> ByTypes = allData
.GroupBy(o => o.Type)
.Select((g, index) => new TableModel()
{
...
IsLastItem = index == (numDataTypes - 1),
});
Then in the ListBox (or ItemsControl) I used a Boolean to Visibility converter (similar to the one posted here Silverlight 4: how to switch control visibility):
<ItemsControl ItemsSource="{Binding ByTypes}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
...
<!-- Seperator between list items -->
<Rectangle Grid.Row="3" Height="20" Visibility="{Binding IsLastItem, Converter={StaticResource VisibileWhenFalseConverter}}" />
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
精彩评论