| 基本操作 > 編集機能 > ダブルクリック時にテキストを選択状態にする |
グリッドでセルをダブルクリックするまたはF2キーを押すと、セルのテキスト全体が選択されます。これは C1FlexGrid のデフォルト動作となりますが、セルに直接入力を行う場合や編集状態のとき入力を行う場合、新たに入力した値を以前の値の最後の位置に挿入することも可能です。C1FlexGrid でカスタム CellFactory クラスを実装して実現できます。たとえば、次のコードでは、連結グリッドで直接入力を行ったとき追記編集を実現する方法を示します。
|
コードのコピー
|
|
|---|---|
Public Sub New() InitializeComponent() Dim p As ICollectionView = Product.GetProducts(100) _flex.ItemsSource = p _flex.CellFactory = New MyCellFactory() End Sub ' カスタムセルファクトリを作成します Public Class MyCellFactory Inherits CellFactory Private originText As String = String.Empty Public Overrides Function CreateCellEditor(grid As C1FlexGrid, cellType As CellType, rng As CellRange) As FrameworkElement Dim control = MyBase.CreateCellEditor(grid, cellType, rng) Dim bdr = TryCast(control, Border) Dim txt = TryCast(bdr.Child, TextBox) originText = txt.Text AddHandler txt.TextChanged, AddressOf txt_TextChanged Return control End Function Private Sub txt_TextChanged(sender As Object, e As TextChangedEventArgs) Dim t = TryCast(sender, TextBox) RemoveHandler t.TextChanged, AddressOf txt_TextChanged t.Text = originText & Convert.ToString(t.Text) t.SelectionStart = t.Text.Length End Sub End Class |
|
|
コードのコピー
|
|
|---|---|
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
_flex.ItemsSource = Product.GetProducts(100);
_flex.CellFactory = new MyCellFactory();
}
}
// カスタムセルファクトリを作成します
public class MyCellFactory : CellFactory
{
string originText = string.Empty;
public override FrameworkElement CreateCellEditor(C1FlexGrid grid, CellType cellType, CellRange rng)
{
var control = base.CreateCellEditor(grid, cellType, rng);
var bdr = control as Border;
var txt = bdr.Child as TextBox;
originText = txt.Text;
txt.TextChanged += txt_TextChanged;
return control;
}
void txt_TextChanged(object sender, TextChangedEventArgs e)
{
var t = (sender as TextBox);
t.TextChanged -= txt_TextChanged;
t.Text = originText + t.Text;
t.SelectionStart = t.Text.Length;
}
}
|
|