If I am styling a list box, sometimes I’ll have a divider between each element. BUT I don’t want that divider below the final element.

Is there some sort of converter trickery I can use to get this? (Silverlight 5)
Answer thanks to the below post:
XAML:
<UserControl
x:Class="SilverlightApplication41.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SilverlightApplication41"
Width="640" Height="480">
<UserControl.Resources>
<local:NotLastItemToVisibilityConverter x:Key="NotLastItemToVisibilityConverter"/>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White">
<Border Background="#EEEDED" HorizontalAlignment="Left" VerticalAlignment="Center">
<ListBox x:Name="_listbox" Background="#FFEEEDED" BorderBrush="#FF585858">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Margin="2">
<TextBlock Text="{Binding}"/>
<Rectangle Fill="Orange" Height="2" VerticalAlignment="Bottom" Margin="1,0,1,-2"
Visibility="{Binding Converter={StaticResource NotLastItemToVisibilityConverter}}"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Border>
</Grid>
</UserControl>
CS:
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;
using System.Collections.Generic;
namespace SilverlightApplication41
{
public partial class MainPage : UserControl
{
public MainPage()
{
// Required to initialize variables
InitializeComponent();
List<string> dataList = new List<string>(){"Ants", "Bats", "Cats", "Dogs", "Entoloma sinuatum"};
((NotLastItemToVisibilityConverter)Resources["NotLastItemToVisibilityConverter"]).DataList = dataList;
_listbox.ItemsSource = dataList;
}
}
public class NotLastItemToVisibilityConverter : IValueConverter
{
public List<string> DataList {get; set;}
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if(DataList.Count == 0 || (string)value == DataList[DataList.Count-1])
{
return Visibility.Collapsed;
}
return Visibility.Visible;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
}
Note: The comparison needs to be an object comparison OR unique strings in the list. Otherwise the list A,B,A would only have a divider after B since “A” == “A”. So this is a bad example using a string but makes the point. I tried casting DataList[DataList.Count-1] to an object but it doesn’t seem that the binding uses the exact string passed in.
Yes there will be some converter trickery.
Bind the visibility of the divider to the list item. Then in the converter check return
trueif it’s not the.Last()item in the list andfalseif it is. You will need to get access to the view model in the converter.I don’t have the code to hand right now, but I’ve done something similar to this to enable/disable buttons.