To get this to work with a ListBox that changes over time, I ended up using MultiBinding:
<DataTemplate x:Key="myItemTemplate"> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding}"/> <TextBlock x:Name="dots" Text="..."/> </StackPanel> <DataTemplate.Triggers> <DataTrigger Value="False"> <DataTrigger.Binding> <MultiBinding Converter="{StaticResource isLastItemInContainerConverter}"> <Binding RelativeSource="{RelativeSource FindAncestor, AncestorType=ListBoxItem}" /> <Binding Path="Items.Count" RelativeSource="{RelativeSource FindAncestor, AncestorType=ListBox}" /> </MultiBinding> </DataTrigger.Binding> <Setter TargetName="dots" Property="Visibility" Value="Collapsed"/> </DataTrigger> </DataTemplate.Triggers> </DataTemplate>
Note: The second binding is only used to receive notification when the list changes.
here is the corresponding MultivalueConverter
public class IsLastItemInContainerConverter : IMultiValueConverter { public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) { DependencyObject item = (DependencyObject)values[0]; ItemsControl ic = ItemsControl.ItemsControlFromItemContainer(item); return ic.ItemContainerGenerator.IndexFromContainer(item) == ic.Items.Count - 1; } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } }
user1859022
source share