dev Gridcontrol根據其cell里面的值顯示不同顏色


要改變cell值得顏色 需要用到cellTemplate和convert

        <DataTemplate x:Key="PercentageCellColorTemplate">
            <dxe:TextEdit Name="PART_Editor" Mask="p2" MaskType="Numeric" MaskUseAsDisplayFormat="True" FontWeight="Bold" Foreground="{Binding Value, Converter=  {StaticResource       colorConvert}}">
            </dxe:TextEdit>
        </DataTemplate>

Foreground="{Binding Value} 這個value就是這個cell里面的值,在convert中可以根據值得情況來返回不同的顏色值

convert類定義形式如下:

    public class ColorConvert : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string returnValue = "Black"; //默認為black
            if (value != null)
            {
                string sValue = value.ToString();
                if (sValue != "")
                {
                    double dValue;
                    if (double.TryParse(value.ToString(), out dValue))
                    {
                        if (dValue > 0)
                            returnValue = "Red";
                        else if (dValue < 0)
                        {
                            returnValue = "Green";
                        }
                        else
                        {
                            returnValue = "Black";
                        }
                    }
                }
            }
            return returnValue;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }

            設置改列的cellTemplate屬性

                <dxg:GridColumn Header="colorTest" FieldName="test" CellTemplate="{StaticResource PercentageCellColorTemplate}">
                </dxg:GridColumn>

通過上面過程Foreground屬性會根據當前cell的值 來得到不同的屬性值 從而顯示不同的顏色。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM