【问题标题】:How to change color of edited cell in Pivot Grid DevExpress如何在 Pivot Grid DevExpress 中更改已编辑单元格的颜色
【发布时间】:2015-10-01 17:45:16
【问题描述】:

我的情况是我需要将已编辑的 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 版。任何的想法?提前致谢!!

【问题讨论】:

    标签: c# wpf devexpress pivot-table


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-10
      • 2019-11-27
      • 1970-01-01
      • 1970-01-01
      • 2020-09-25
      • 2013-10-30
      • 2015-08-30
      • 2017-04-19
      相关资源
      最近更新 更多