【问题标题】:How to colorize DevExpress WPF GridControl Row?如何为 DevExpress WPF GridControl 行着色?
【发布时间】:2015-10-23 21:12:53
【问题描述】:

我有一个 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。但是我想着色,如果
年龄

我该怎么做?

【问题讨论】:

  • 我不熟悉 devexpress GridControl。 gridControl 上是否有 RowStyle 依赖属性?您是否为此控件绑定了 ItemsSource?如果是这样,每行的 viewModel 上会显示年龄、收入和部门吗?

标签: c# wpf data-binding devexpress


【解决方案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

<dxg:GridColumn.CellStyle>
    <Style TargetType="{x:Type dxg:CellContentPresenter}">
        <Setter Property="TextBlock.Foreground" Value="Black" />
        <Style.Triggers>
            <DataTrigger Binding="{Binding Path=Data.SomeBooleanValue}" Value="True">
                <Setter Property="Background" Value="Lime" />
                <Setter Property="TextBlock.Foreground" Value="Black" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</dxg:GridColumn.CellStyle>

【讨论】:

    【解决方案2】:

    在资源部分添加以下行:

    <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}">
    

    对其他列也重复相同的操作

    【讨论】:

    【解决方案3】:

    你需要编辑 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>
    

    【讨论】:

      【解决方案4】:

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

      SolidColorBrush scb = new BrushConverter().ConvertFromString("#FFFF0000") as SolidColorBrush;//Red
              DevExpress.Xpf.Core.ConditionalFormatting.Format frm = new DevExpress.Xpf.Core.ConditionalFormatting.Format { Background = scb };
              TreeListView.FormatConditions.Add
                  (
                      new FormatCondition { FieldName = "Name", ApplyToRow = true, Format = frm , Expression = "[ZoneId] = 1" }
                  );
      

      【讨论】:

        【解决方案5】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-11-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多