Change style of last item in ListBox

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

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



        
3条回答
  •  鱼传尺愫
    2020-12-06 07:48

    to get this to work w/ a ListBox that changes over time I ended up using a MultiBinding:

    
        
            
            
        
        
            
                
                    
                        
                        
                    
                
                
            
        
    
    

    Note: the second binding is only used to get notified 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();
        }
    }
    

提交回复
热议问题