Change style of last item in ListBox

后端 未结 3 1501
生来不讨喜
生来不讨喜 2020-12-06 07:04

I have listbox control which has list of colors. Here is code and Image:



        
3条回答
  •  粉色の甜心
    2020-12-06 07:50

    This can be achieved through converter which do the work of finding if its last item in the listbox -

    Converter

    public class IsLastItemInContainerConverter : IValueConverter
    {
       public object Convert(object value, Type targetType,
                             object parameter, CultureInfo culture)
       {
           DependencyObject item = (DependencyObject)value;
           ItemsControl ic = ItemsControl.ItemsControlFromItemContainer(item);
    
           return ic.ItemContainerGenerator.IndexFromContainer(item)
                   == ic.Items.Count - 1;
       }
    
       public object ConvertBack(object value, Type targetType,
                                 object parameter, CultureInfo culture)
       {
          throw new NotImplementedException();
       }
    }
    

    And using that you can set the DataTemplate in your xaml class like this -

    
    
    
    

提交回复
热议问题