簡體   English   中英

如何為DevExpress WPF GridControl行着色?

[英]How to colorize DevExpress WPF GridControl Row?

我有一個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。 但是我想着色
年齡<= 0或收入<0或部門<= 0(綁定數據后行變成紅色)

我怎樣才能做到這一點?

您可以將其用於整個行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>

在資源部分添加以下行:

<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類的名稱空間

最后在表格視圖中設置行樣式綁定:

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

對其他列也重復相同的步驟

您需要編輯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>

您可以通過編程將格式設置為行。 它也適用於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" }
            );

您可以從這里開始: 如何有條件地應用樣式

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM