如何在Pivot Grid DevExpress中更改编辑单元格的颜色



我的情况是,我需要在Pivot网格中绘制绿色的单元格。我已经尝试将pivot网格订阅到CustomCellAppearance事件,但当然,这将绘制整个数据表。我正在处理LostFocus事件的编辑部分,这意味着单元格在失去焦点时被编辑。在这种情况下,我需要绘制单元格。

这是我的PivotGridView的一部分。Xaml代码(其中定义了pivot网格):

<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版本。任何想法?提前感谢!!

可以使用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));
}

最新更新