1

我创建了一个多行多列的数据网格。其中一列是用户可以更改的字段大小列表。

我正在根据旧值检查新值,如果新值小于旧值,我告诉用户这是无效的,然后我想放回旧值并将焦点重置到该单元格。

我的LostFocus活动中有这一行:

System.Windows.Controls.TextBox tbNewSize = 
    (System.Windows.Controls.TextBox)dtgCell.Content;

当我单击单元格时,该LostFocus事件被调用并且工作正常。但是,当我尝试重新聚焦到单元格时,我收到一条错误消息

“无法将‘System.Windows.Controls.TextBlock’类型的对象转换为‘System.Windows.Controls.TextBox’类型。”

我该如何纠正这个问题?

这是我的 XAML 代码:

<DataGrid HeadersVisibility="Column" Name="dtGrid" Loaded="GridLoaded" AutoGenerateColumns="False" IsReadOnly="False" VirtualizingPanel.IsVirtualizing="False" Height="365" Width="530" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="54,74,0,0" BorderThickness="1,1,0,1" BorderBrush="Black">
<DataGrid.Columns>
    <DataGridTextColumn Header="Field" Binding="{Binding Field, Mode=TwoWay}" Width="209" IsReadOnly="True" />
    <DataGridTextColumn Header="Size" Binding="{Binding Size, Mode=TwoWay}" Width="89"/>
    <DataGridCheckBoxColumn Header="Right Justify" Binding="{Binding RightJustify, Mode=TwoWay}" Width="55" />
    <DataGridCheckBoxColumn Header="Left Justify" Binding="{Binding LeftJustify, Mode=TwoWay}"  Width="55"  />
    <DataGridCheckBoxColumn Header="Left Zero Fill" Binding="{Binding LeftZeroFill, Mode=TwoWay}" Width="55" />
    <DataGridCheckBoxColumn Header="Right Zero Fill" Binding="{Binding RightZeroFill, Mode=TwoWay}" Width="65" />
</DataGrid.Columns>
<DataGrid.ColumnHeaderStyle>
    <Style TargetType="DataGridColumnHeader">
        <Setter Property="ContentTemplate">
            <Setter.Value>
                <DataTemplate>
                    <TextBlock TextWrapping="Wrap" Text="{Binding}"></TextBlock>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</DataGrid.ColumnHeaderStyle>
<DataGrid.Resources>
    <Style TargetType="{x:Type DataGridCell}">
        <Style.Triggers>
            <Trigger Property="DataGridCell.IsSelected" Value="True">
                <Setter Property="Background" Value="#FF9DF3D6" />
                <Setter Property="Foreground" Value="#000000" />
            </Trigger>
        </Style.Triggers>
        <EventSetter Event="PreviewMouseLeftButtonDown" Handler="DataGridCell_PreviewMouseLeftButtonDown" />
        <EventSetter Event="LostFocus" Handler="DataGridCell_OnCellLostFocus" />
    </Style>
</DataGrid.Resources>

这是我的 C# 代码:

private void DataGridCell_OnCellLostFocus(object sender, RoutedEventArgs e)
{
    System.Windows.Controls.DataGridCell dtgCell = (System.Windows.Controls.DataGridCell)sender;

    if (dtgCell.Column.Header.ToString() == "Size")
    {
        System.Windows.Controls.TextBox tbNewSize = (System.Windows.Controls.TextBox)dtgCell.Content;
        Int32 intNewSize = Convert.ToInt32(tbNewSize.Text);
        Int32 intCurrSize = Convert.ToInt32(strFieldInfoOrig[dtGrid.Items.IndexOf(dtGrid.CurrentItem), 1]);

        if (intNewSize < intCurrSize)
        {
            string strMsg;

            strMsg = "New size, " + intNewSize.ToString() + " is smaller then the original size, " + intCurrSize.ToString();
            strMsg += Environment.NewLine;
            strMsg += "Due to potential data loss, this is not allowed.";
            System.Windows.MessageBox.Show(strMsg);
            //dtgCell.Content = intCurrSize.ToString();
            dtgCell.Focus();
        }
    }
}
4

3 回答 3

1

您可以处理CellEditEnding事件。

<DataGrid AutoGenerateColumns="False"
          CellEditEnding="DataGrid_CellEditEnding"
          ...
          >
    <DataGrid.Columns>
        <DataGridTextColumn Header="Size" Binding="{Binding Size, Mode=TwoWay}" .../>

        ...

    </DataGrid.Columns>

    ...
    
</DataGrid>

背后的代码

private void DataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e) 
{
    if (e.EditAction == DataGridEditAction.Commit)
    {
        if (e.Column is DataGridBoundColumn)
        {
            DataGridBoundColumn column = (DataGridBoundColumn)e.Column;
            if (column.Header.ToString() == "Size")
            {
                string oldValue = e.Row.DataContext.GetType().GetProperty("Size")
                                   .GetValue(e.Row.DataContext).ToString();
                TextBox element = e.EditingElement as TextBox;
                string newValue = element.Text;
                int oldSize = int.Parse(oldValue);
                int newSize = int.Parse(newValue);
                if (newSize < oldSize)
                {
                    string strMsg = "New size, " + newValue + ", is smaller then the original size, "
                                  + oldValue + ".\nDue to potential data loss, this is not allowed.";
                    MessageBox.Show(strMsg);
                    element.Text = oldValue;
                    e.Cancel = true;
                }
            }
        }
    }
}

设置e.Cancel = true使单元格保持在编辑模式。

于 2017-02-26T09:31:08.357 回答
0

发生这种情况是因为在正常模式下和编辑时DataGridTextColumn显示 a 。因此,当此单元格失去焦点时,会以正常模式返回,因此其内容将为 a而不是,因此显示异常。TextBlockTextBoxDataGridTextColumnTextBlockTextBox

所以,试着投进TextBlock而不是TextBox

于 2017-02-26T05:09:32.410 回答
0

您正在尝试将 aTextBlock转换为 a TextBox,这显然是行不通的。但是,如果您只是尝试始终转换为TextBlock这样的:

System.Windows.Controls.TextBlock tbNewSize = (System.Windows.Controls.TextBlock)dtgCell.Content;

...这也行不通。这是因为Content单元格的 可能是 aTextBox aTextBlock取决于单元格当前是否处于编辑模式。

您可以使用as运算符尝试转换为 a TextBox,如果转换失败,则将Content属性转换为 a TextBlock

private void DataGridCell_OnCellLostFocus(object sender, RoutedEventArgs e)
{
    System.Windows.Controls.DataGridCell dtgCell = (System.Windows.Controls.DataGridCell)sender;
    if (dtgCell.Column.Header.ToString() == "Size")
    {
        string text = null;
        System.Windows.Controls.TextBox tbNewSize = dtgCell.Content as System.Windows.Controls.TextBox;
        if (tbNewSize != null)
        {
            text = tbNewSize.Text;
        }
        else
        {
            System.Windows.Controls.TextBlock tb = dtgCell.Content as System.Windows.Controls.TextBlock;
            if (tb != null)
                text = tb.Text;
        }
        Int32 intNewSize = Convert.ToInt32(text);
        Int32 intCurrSize = Convert.ToInt32(strFieldInfoOrig[dtGrid.Items.IndexOf(dtGrid.CurrentItem), 1]);

        if (intNewSize < intCurrSize)
        {
            string strMsg;

            strMsg = "New size, " + intNewSize.ToString() + " is smaller then the original size, " + intCurrSize.ToString();
            strMsg += Environment.NewLine;
            strMsg += "Due to potential data loss, this is not allowed.";
            System.Windows.MessageBox.Show(strMsg);
            //dtgCell.Content = intCurrSize.ToString();
            dtgCell.Focus();
        }
    }
}
于 2017-02-26T11:12:54.977 回答