2012-10-19 121 views
1

我有一個GridControl:如何爲DevExpress WPF 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 =列表。但我想,如果着色
年齡< = 0或收入< 0或部門< = 0(行上色結合數據後紅)

我該怎麼辦呢?

+0

我不熟悉devexpress GridControl。 GridControl中是否存在RowStyle依賴項屬性?你有這個控件綁定的ItemsSource嗎?如果是這樣,每個行的viewModel會有年齡,收入和Dept嗎? – Sisyphe

回答

5

您可以使用此整行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> 
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類:本地的ColorValueConverter類的命名空間

最後設定的行樣式綁定的表格視圖:

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

重複相同的其他列以及

+1

請注意,如果您的XAML中尚未包含此名稱空間聲明,請將其添加:xmlns:dxgt =「http://schemas.devexpress.com/winfx/2008/xaml/grid/themekeys」 – Jon

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> 

1

您可以設置格式以編程行。它也適用於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" } 
      ); 
相關問題