作成日: 2025/07/07 最終更新日: 2025/07/07
文書種別
不具合
状況
回避方法有り
詳細
GcSpreadGridの数値型セルに数値を設定してアプリケーションを実行します。該当の数値型セルに非編集状態から入力済みの数値と同じ値を入力すると、セルの編集が開始され値が不正に選択状態になります。値が選択されずにキャレットの位置が値の後ろになるのが本来の動作です。
例えば、「1」が入力されているセルに対して「10」を入力しようとして「1」をタイプすると、セルの編集が開始され「1」が選択状態になります。続けて「0」をタイプしようとしますが、「1」が選択状態になっているため数値が置き換わり入力後の値が「0」になります。
回避方法
数値が入力された時にキャレットの位置が正しくなるよう、数値型セルの動作を上書きして回避が可能です。
[MainWindow.xaml]
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:NumberCellTest"
xmlns:sg="http://schemas.grapecity.com/windows/spreadgrid/2012" x:Class="NumberCellTest.MainWindow"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<sg:NumberCellType x:Key="NumberCellTypeWithCaretControl">
<sg:NumberCellType.EditElementStyle>
<Style TargetType="sg:NumberEditElement">
<EventSetter Event="PreviewKeyDown" Handler="NumberEditElement_PreviewKeyDown"/>
</Style>
</sg:NumberCellType.EditElementStyle>
</sg:NumberCellType>
</Window.Resources>
<Grid>
<sg:GcSpreadGrid x:Name="SpreadGrid"/>
</Grid>
</Window> [MainWindow.xaml.cs]
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var numberCellType = Resources["NumberCellTypeWithCaretControl"] as NumberCellType;
foreach (var column in SpreadGrid.Columns)
{
column.CellType = numberCellType;
}
SpreadGrid.SelectionBorder = new BorderLine(Colors.Red, BorderLineStyle.Thick);
SpreadGrid.Focus();
}
public static bool IsDigitKey(Key key)
{
bool isMainKeyboardDigit = key >= Key.D0 && key <= Key.D9;
bool isNumPadDigit = key >= Key.NumPad0 && key <= Key.NumPad9;
return isMainKeyboardDigit || isNumPadDigit;
}
private async void NumberEditElement_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (!(sender is NumberEditElement textBox) || !IsDigitKey(e.Key))
{
return;
}
await Task.Delay(30);
if (textBox.CaretIndex == 0 &&
textBox.SelectionLength == textBox.Text.Length &&
textBox.Text.Length == 1)
{
textBox.CaretIndex = 1;
}
}
}