关于DevExpress WPF DataGrid 如何根据某列内容来改变整行颜色

<Window x:Class="ConditionalRowFormatting.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid" 
    xmlns:dxgt="http://schemas.devexpress.com/winfx/2008/xaml/grid/themekeys"
    xmlns:local="clr-namespace:ConditionalRowFormatting" 
    Title="Window1" 
    Height="430" 
    Width="610">

    <Window.Resources>
        <ResourceDictionary>
            <local:SimpleDataList x:Key="simpleSource" InitCount="25" />
            <Style x:Key="BackgroundStyle" BasedOn="{StaticResource {dxgt:GridRowThemeKey ResourceKey=RowStyle}}" TargetType="{x:Type dxg:GridRowContent}">
          //重点:这里Binding的是 Row.Int 也就是行中哪一列的值. <Setter Property="Background" Value="{Binding Path=Row.Int, Converter={local:ColorValueConverter MaxValue=25}}" /> </Style> </ResourceDictionary> </Window.Resources> <Grid> <dxg:GridControl ItemsSource="{StaticResource simpleSource}" AutoGenerateColumns="AddNew"> <dxg:GridControl.View> <dxg:TableView AutoWidth="True" RowStyle="{StaticResource BackgroundStyle}" /> </dxg:GridControl.View> </dxg:GridControl> </Grid> </Window>

值转换器:

public class ColorValueConverter : MarkupExtension, IValueConverter {
        public int MaxValue { get; set; }
        #region IValueConverter Members
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
            int count = MaxValue - (int)value;
            return new SolidColorBrush(GetGradientColor(count));
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
            return null;
        }
        #endregion
        public override object ProvideValue(IServiceProvider serviceProvider) {
            return this;
        }
        private Color GetGradientColor(int count) {
            Color color = Color.FromRgb(0xff, 0xad, 0x5d);
            byte r = (byte)(color.R + (0xff - color.R) * count / MaxValue);
            byte g = (byte)(color.G + (0xff - color.G) * count / MaxValue);
            byte b = (byte)(color.B + (0xff - color.B) * count / MaxValue);
            return Color.FromArgb(255, r, g, b);
        }
    }
 

转载于:https://www.cnblogs.com/blueice-007/p/3654081.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值