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

时间:2015-10-01 17:45:17

标签: c# wpf devexpress pivot-table

我的情况是我需要绘制已编辑的Pivot Grid中的单元格。我试图将枢轴网格订阅到CustomCellAppearance事件,但当然会绘制整个数据表。我正在使用LostFocus事件处理编辑部分,这意味着在失去焦点时编辑单元格。在那种情况下,我需要画细胞。

这是我的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。任何的想法?在此先感谢!!

1 个答案:

答案 0 :(得分: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));
}