2015-10-01 103 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)); 
} 
相關問題