2015-10-01 79 views
0

我的情况是,我需要绘制绿色编辑的透视网格中的单元格。我试图订阅pivot网格到CustomCellAppearance事件,但是当然这会绘制整个数据表。我使用LostFocus事件处理编辑部分,这意味着单元格在失去焦点时会被编辑。在这种情况下,我需要绘制单元格。如何更改透视网格中编辑的单元格的颜色DevExpress

这是一段我PivotGridView.xaml代码(其中枢纽电网的定义):

<dxpg:PivotGridControl x:Name="PivotGridControl1" ChartSelectionOnly="False" 
           CellSelectedBackground="LightSlateGray" CellBackground="GhostWhite" Background="LightBlue" 
           ValueSelectedBackground="LightSlateGray" 
           CellTotalBackground="Linen" ValueTotalBackground="LightSkyBlue" ValueBackground="LightSteelBlue" 
           ValueTotalSelectedBackground="DeepSkyBlue" 
           Width="Auto" Height="430" Margin="0,-1,-8,40"> 

      <dxpg:PivotGridControl.Fields> 
       <dxpg:PivotGridField Area="DataArea" Caption="Amount" FieldName="amount"> 
        <dxpg:PivotGridField.CellTemplate> 
         <DataTemplate> 
          <dxe:TextEdit x:Name="edit" DisplayFormatString="c2" HorizontalContentAlignment="Right" 
              EditMode="InplaceInactive"        
              Mask="[0-9]*.[0-9]{0,2}" 
              MaskType="RegEx" 
              EditValue="{Binding Value, Mode=OneWay}" 
              LostFocus="TextEdit_LostFocus" 
              FocusVisualStyle="{StaticResource TextFocused}"> 
           <dxe:TextEdit.InputBindings> 
            <MouseBinding MouseAction="LeftClick" Command="{x:Static local:PivotTableView.StartEdit}" CommandParameter="{Binding ElementName=edit}" /> 
           </dxe:TextEdit.InputBindings> 
          </dxe:TextEdit> 
         </DataTemplate> 
        </dxpg:PivotGridField.CellTemplate> 

       </dxpg:PivotGridField> 
       <dxpg:PivotGridField Area="RowArea" Caption="Item" FieldName="item" /> 
       <dxpg:PivotGridField Area="ColumnArea" Caption="Name" FieldName="name" /> 
      </dxpg:PivotGridControl.Fields> 

     </dxpg:PivotGridControl> 

这是处理代码:

void TextEdit_LostFocus(object sender, RoutedEventArgs e) 
    { 
     EditValue(sender); 
    } 

    static void EditValue(object sender) 
    { 
     TextEdit edit = (sender as TextEdit); 

     if (edit == null || edit.DataContext as CellsAreaItem == null) return; 
     CellsAreaItem item = edit.DataContext as CellsAreaItem; 
     decimal newValue; 
     decimal oldValue; 
     if (edit.EditValue != null && decimal.TryParse(edit.EditValue.ToString(), out newValue)) 
     { 

      if (item.Value == null || !decimal.TryParse(item.Value.ToString(), out oldValue)) 
       return; 
      PivotGridControl pivotGrid = FindParentPivotGrid((DependencyObject)sender); 

      if (pivotGrid == null) 
       return; 
      PivotGridField fieldExtendedPrice = pivotGrid.Fields["amount"]; 
      PivotDrillDownDataSource ds = pivotGrid.CreateDrillDownDataSource(item.ColumnIndex, item.RowIndex); 
      decimal difference = newValue - oldValue; 
      decimal factor = (difference == newValue) ? (difference/ds.RowCount) : (difference/oldValue); 

      for (int i = 0; i < ds.RowCount; i++) 
      { 
       decimal value = Convert.ToDecimal(ds[i][fieldExtendedPrice]); 
       ds[i][fieldExtendedPrice] = (double)((value == 0m) ? factor : value * (1m + factor));//(double)newValue; 
      } 

      pivotGrid.RefreshData(); 

     } 
    } 

我使用的版本13.2 。任何想法?提前致谢!!

回答

0

您可以使用CustomCellAppearance事件。您可以将已编辑单元格的字段值存储到某个位置,并检查CustomCellAppearance事件是否存储当前单元格的字段值。
这里是例子:

static private List<Tuple<string, string>> _editedCells = new List<Tuple<string,string>>(); 

static void EditValue(object sender) 
{ 
    TextEdit edit = (sender as TextEdit); 

    if (edit == null || edit.DataContext as CellsAreaItem == null) return; 
    CellsAreaItem item = edit.DataContext as CellsAreaItem; 
    decimal newValue; 
    decimal oldValue; 
    if (edit.EditValue != null && decimal.TryParse(edit.EditValue.ToString(), out newValue)) 
    { 

     if (item.Value == null || !decimal.TryParse(item.Value.ToString(), out oldValue)) 
      return; 
     PivotGridControl pivotGrid = FindParentPivotGrid((DependencyObject)sender); 

     if (pivotGrid == null) 
      return; 
     PivotGridField fieldExtendedPrice = pivotGrid.Fields["amount"]; 
     PivotDrillDownDataSource ds = pivotGrid.CreateDrillDownDataSource(item.ColumnIndex, item.RowIndex); 
     decimal difference = newValue - oldValue; 
     decimal factor = (difference == newValue) ? (difference/ds.RowCount) : (difference/oldValue); 

     for (int i = 0; i < ds.RowCount; i++) 
     { 
      decimal value = Convert.ToDecimal(ds[i][fieldExtendedPrice]); 
      ds[i][fieldExtendedPrice] = (double)((value == 0m) ? factor : value * (1m + factor));//(double)newValue; 
     } 

     //Store the fields values. 
     var cellInfo = PivotGridControl1.GetCellInfo(item.ColumnIndex, item.RowIndex); 

     string itemValue = (string)cellInfo.GetFieldValue(PivotGridControl1.Fields["item"]); 
     string nameValue = (string)cellInfo.GetFieldValue(PivotGridControl1.Fields["name"]); 

     var editedCell = new Tuple<string, string>(itemValue, nameValue); 

     if (!_editedCells.Contains(editedCell)) 
      _editedCells.Add(editedCell); 

     pivotGrid.RefreshData(); 
    } 
}  

private void PivotGridControl1_CustomCellAppearance(object sender, PivotCustomCellAppearanceEventArgs e) 
{ 
    //Check for field values. 
    string itemValue = (string)e.GetFieldValue(PivotGridControl1.Fields["item"]); 
    string nameValue = (string)e.GetFieldValue(PivotGridControl1.Fields["name"]); 

    var editedCell = new Tuple<string, string>(itemValue, nameValue); 

    if (_editedCells.Contains(editedCell)) 
     e.Background = new SolidColorBrush(Color.FromRgb(0, 255, 0)); 
} 
相关问题