How to check if a row has odd number?

后端 未结 3 2035
无人共我
无人共我 2021-01-15 02:03

I\'m trying to set different color for odd rows using XAML.

The datagrid in question has 3 different types of data, which I want to color differently, and simply cha

3条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-15 02:54

    Almost every answer use AlternationCount="2" but I find it a little bit too limiting. On my side I use something like AlternationCount="{ Binding MainData.ProjColl.Count}" in order to number my rows until the end ( take care it starts at 0 ! ).

    In this case I need a value Converter as mentioned by @Danny Varod.

    I use the converter to alternate the color of the rows ( nearly answering the question )

    public class IsEvenConverter : IValueConverter
    {
        public object Convert(object value, Type targetType,
            object parameter, CultureInfo culture)
        {
            bool res = false;
            int? val = value as int?;
            if (null != val)
                res = (0 == (val % 2));
            return res;
        }     
        ...
    }
    

    And the calling XAML

    
        ...
        
        
    
    
    
        
        ...
        
    
    
    

    and some discrete screenshots

    Another part of the same code: Simple way to display row numbers on WPF DataGrid

提交回复
热议问题