如何着色DevExpress WPF GridControl Row?

时间:2012-10-19 07:48:37

标签: c# wpf data-binding devexpress

我有一个GridControl:


 <dxg:GridControl Name="grd"  Height="270">
            <dxg:GridControl.Columns>
                <dxg:GridColumn Header="Id" FieldName="Id" AllowEditing="True" Width="30"/>
                <dxg:GridColumn Header="Name" FieldName="Name" AllowEditing="False" />
                <dxg:GridColumn Header="SurName" FieldName="SurName" AllowEditing="False" />
                <dxg:GridColumn Header="Age" FieldName="Age" CellStyle="{StaticResource customCellStyle}"  AllowEditing="False" />
                <dxg:GridColumn Header="Income" FieldName="Income" AllowEditing="False" />
                <dxg:GridColumn Header="Dept" FieldName="Dept" AllowEditing="False" />
            </dxg:GridControl.Columns>

        </dxg:GridControl>

我一直在绑定itemsource = List。但如果我想要着色 年龄&lt; = 0或收入&lt; 0或Dept&lt; = 0(绑定数据后行颜色为红色)

我该怎么做?

5 个答案:

答案 0 :(得分:5)

您可以将其用于整行[{1}}:

style

和单元格<dxg:TableView.RowStyle> <Style BasedOn="{StaticResource {dxgt:GridRowThemeKey ResourceKey=RowStyle}}" TargetType="{x:Type dxg:GridRowContent}"> <Setter Property="Background" Value="{Binding Path=DataContext.Colour}" /> </Style> </dxg:TableView.RowStyle>

style

答案 1 :(得分:1)

在资源部分添加以下行:

<Style x:Key="ConditionalRowStyle"  BasedOn="{StaticResource {dxgt:GridRowThemeKey ResourceKey=RowStyle}}" TargetType="{x:Type dxg:GridRowContent}">
        <Setter Property="Background" Value="{Binding Path=DataContext.Age, Converter={local:ColorValueConverter}}"/>
</Style>

然后创建一个实现IValueConverter ColorValueConverter

的类
 public class ColorValueConverter : MarkupExtension, IValueConverter
    {


        #region IValueConverter Members
        public object Convert(object value, Type targetType,
        object parameter, CultureInfo culture)
        {
      /* your conditions here i.e Age<=0 */     
       if ((Int64)value <= 0)
         /*Return red color*/
                return Brushes.Red;
            else
        /*Return the default color*/                  
                return Brushes.White;
        }

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

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            return this;
        }
    }
  

将xmlns:local设置为ColorValueConverter类的命名空间

最后在表视图中设置Row样式绑定:

 <dxg:TableView Name="tableView1" RowStyle="{StaticResource ConditionalRowStyle}">

对其他列重复相同的操作

答案 2 :(得分:1)

您需要编辑TableView

<dxg:GridControl.View>
<dxg:TableView x:Name="tv_grd" ShowGroupPanel="False" AutoWidth="True"                  
    IsTotalSummaryMenuEnabled="False" RowUpdated="tv_grd_RowUpdated" >
    <dxg:TableView.FormatConditions>                        
        <dxg:FormatCondition ApplyToRow="True" Expression="[Age] &lt; '0'" FieldName="Age" PredefinedFormatName="LightRedFillWithDarkRedText"/>
    </dxg:TableView.FormatConditions>
    <dxg:TableView.FormatConditions>                        
        <dxg:FormatCondition ApplyToRow="True" Expression="[Income] &lt; '0'" FieldName="Income" PredefinedFormatName="LightRedFillWithDarkRedText"/>
    </dxg:TableView.FormatConditions>
        <dxg:FormatCondition ApplyToRow="True" Expression="[Dept] &lt; '0'" FieldName="Dept" PredefinedFormatName="LightRedFillWithDarkRedText"/>
    </dxg:TableView.FormatConditions>
</dxg:TableView>

答案 3 :(得分:1)

您可以通过编程方式将格式设置为行。它也适用于gridView。

var q = (from o in Mngr 
            from v in VWE
                .Where(w=>
                    w.Mdl == o.CM &&
                    w.Vrs != o.CV)
                    where o.Mod == 1
                    select o).distinct();

答案 4 :(得分:0)

您可以从这里开始:How to Conditionally Apply Styles