在"撤消更改"之后,DataGridCell中的内部TextBlock(TextBox)中出现意外行为

时间:2016-03-15 08:48:00

标签: c# wpf datagrid

我的DataGrid中有可编辑的单元格,我需要创建" 撤消更改"按钮,将原始值设置为单元格 TextBlock

My DataGrid

我在更改TextBlock的值后更改颜色 I change color after changing value of TextBlock

点击"撤消更改"按键 Clicking "undo changes" button

现在有预期Now there is expected value

但点击单元格后,有意外" "值 But after clicking on cell, there is "new" value

Unexpected "new" value

DataGridCell是否有可能两个不同的 TextBlocks(或TextBoxes)?如果是这样,为什么有可能,其中一个重写另一个?

以下是我执行代码的部分内容"撤消更改"

private void ContextMenuDeleteItemClick(object sender, RoutedEventArgs e)
{
  var cellInfo = this.CurrentCell;
  this.ScrollIntoView(cellInfo.Item);
  var row = this.ItemContainerGenerator.ContainerFromItem(cellInfo.Item) as DataGridRow;
  if (row != null)
  {
    FrameworkElement element = cellInfo.Column.GetCellContent(cellInfo.Item);
    KeyValuePair<DataGridColumn, DataGridRow> key = new KeyValuePair<DataGridColumn, DataGridRow>(cellInfo.Column, row);
    if (originalCellValues.ContainsKey(key))
    {   
      ((element.Parent as DataGridCell).Content as TextBlock).Text = originalCellValues[key];
      (element.Parent as DataGridCell).Style = (element.Parent as DataGridCell).Column.CellStyle;
    }
  }
}

当我尝试在调试时捕获此行为时,在我的 OnBeginningEdit 中值很好,然后突然在 OnCellEditEnding 值出现意外

在我的OnBeginningEdit中,我保存原始值

protected override void OnBeginningEdit(DataGridBeginningEditEventArgs e)
{
  if (!this.editableColumns[e.Column.DisplayIndex])
  {
    if (!e.Row.IsNewItem)
    {
      e.Cancel = true;
    }
  }

  FrameworkElement element = e.Column.GetCellContent(e.Row);
  KeyValuePair<DataGridColumn, DataGridRow> key = new KeyValuePair<DataGridColumn, DataGridRow>(e.Column, e.Row);
  if (!originalCellValues.ContainsKey(key))
  {
    originalCellValues.Add(key, ((element.Parent as DataGridCell).Content as TextBlock).Text);
  }
}

在我的OnCellEditEnding中,我改变了单元格的样式 - 如果编辑了颜色单元格

protected override void OnCellEditEnding(DataGridCellEditEndingEventArgs e)
{
  if ((e.EditingElement as TextBox).Text != originalCellValues[new KeyValuePair<DataGridColumn, DataGridRow>(e.Column, e.Row)])
  {
    FrameworkElement element = e.Column.GetCellContent(e.Row);
    (element.Parent as DataGridCell).Style = CreateStyle("RequiredColor", e.Column.CellStyle);
   }
  else
  {
    FrameworkElement element = e.Column.GetCellContent(e.Row);
    (element.Parent as DataGridCell).Style = e.Column.CellStyle;
  }
}

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。我添加了Mode = TwoWay,其中我绑定了TextBlock值的属性。默认情况下它的行为就像是TwoWay,但在这种情况下不行。真的很奇怪。

<Controls:FilterableDataGrid Grid.Row="1" 
                    ItemsSource="{Binding GarantList, UpdateSourceTrigger=PropertyChanged}"
                    AutoGenerateColumns="False"
                    CanUserAddRows="True"
                    CanUserDeleteRows="True"
                    CanUserReorderColumns="True"
                    CanUserResizeColumns="True"
                    CanUserResizeRows="False"
                    CanUserSortColumns="True"
                    HeadersVisibility="Column"
                    SelectionUnit="CellOrRowHeader"
                    SelectedRow="{Binding ActualRow, Mode=TwoWay}"
                    ScrollViewer.CanContentScroll="True"
                    AlternatingRowBackground="{StaticResource AlternatingRowBackground}"
                    UserContextMenu="{Binding DataGridContextMenu}"
                    IsSaveChanges="{Binding IsSaveChanges, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">
            <Controls:FilterableDataGrid.Columns>
              <DataGridTemplateColumn x:Name="Column1" Width="{StaticResource CheckColumnWidth}" Header="{x:Static localization:Resources.Label_ColumnCheck}" CellStyle="{StaticResource CheckColumnStyle}"/>
              <Controls:DataGridTextColumn x:Name="Column2" Header="{x:Static localization:Resources.Label_Identifikace}" Width="{StaticResource ShortTextColumnWidth}" CellStyle="{StaticResource C_SPStyle}" Binding="{Binding GEx}" SortMemberPath="GEx" IsEditable="False" />
              <Controls:DataGridTextColumn x:Name="Column3" Header="{x:Static localization:Resources.Label_IndAktiv}" Width="{StaticResource IndicationColumnWidth}" CellStyle="{StaticResource IndicationColumnStyle}" Binding="{Binding IndAktiv}" IsEditable="True" />
              <Controls:DataGridTextColumn x:Name="Column4" Header="{x:Static localization:Resources.Label_Jmeno}" Width="{StaticResource ShortTextColumnWidth}" CellStyle="{StaticResource ShortTextColumnStyle}" Binding="{Binding JmenoUz, Mode=TwoWay}" IsEditable="True" />
              <Controls:DataGridTextColumn x:Name="Column5" Header="{x:Static localization:Resources.Label_Telefon}" Width="{StaticResource ShortTextColumnWidth}" CellStyle="{StaticResource ShortTextColumnStyle}" Binding="{Binding TlfUz, Mode=TwoWay}" IsEditable="True" />
              <Controls:DataGridTextColumn x:Name="Column6" Header="{x:Static localization:Resources.Label_Mobil}" Width="{StaticResource ShortTextColumnWidth}" CellStyle="{StaticResource ShortTextColumnStyle}" Binding="{Binding MobilUz}" IsEditable="True" />
              <Controls:DataGridTextColumn x:Name="Column7" Header="{x:Static localization:Resources.Label_Mail}" Width="{StaticResource ShortTextColumnWidth}" CellStyle="{StaticResource ShortTextColumnStyle}" Binding="{Binding MailUz}" IsEditable="True" />
              <Controls:DataGridTextColumn x:Name="Column8" Header="{x:Static localization:Resources.Label_PristupovaUroven}" Width="{StaticResource ShortTextColumnWidth}" CellStyle="{StaticResource ShortTextColumnStyle}" Binding="{Binding CPu}" IsEditable="True" />
            </Controls:FilterableDataGrid.Columns>
          </Controls:FilterableDataGrid>

这里是绑定:

<Controls:DataGridTextColumn x:Name="Column5" Header="{x:Static localization:Resources.Label_Telefon}" Width="{StaticResource ShortTextColumnWidth}" CellStyle="{StaticResource ShortTextColumnStyle}" Binding="{Binding TlfUz, Mode=TwoWay}" IsEditable="True" />