2

SL4に単純なDataGridTextColumn列を持つDataGridがあります。

セルが編集可能なTextBoxに変わるとすぐに、DataGridCell内のすべてのテキストを選択するためにさまざまな方法を試しました。

以下のコードは私の最後の試みでした。

デバッグでTextBoxを調べると、SelectedTextプロパティがTextプロパティと等しいことがわかります。したがって、問題はTextBoxではありません。後でテキストの選択が解除されているようです。

public void PreparingCellForEdit(DataGridPreparingCellForEditEventArgs e)
    {
        var textBox = e.EditingElement as TextBox;
        if (textBox != null && !string.IsNullOrEmpty(textBox.Text))
        {
            textBox.GotFocus += (s, e2) =>
                {
                    {
                        textBox.SelectAll();
                    }
                };
        }
    }

テキストを選択したままにして、選択したテキストを含むTextBoxをユーザーに表示する方法はありますか?

PS私はCliburn.Microを使用してPreparingCellForEditイベントをアタッチしています。

4

2 に答える 2

2

私にとってうまくいくのは次のとおりです。

public void PreparingCellForEdit(DataGridPreparingCellForEditEventArgs e)
{
    var textBox = e.EditingElement as TextBox;
    if (textBox != null)
    {
        textBox.Dispatcher.BeginInvoke(() => textBox.SelectAll());
    }
}
于 2011-08-31T13:10:34.420 に答える
0

ある程度の解決策は、イベントTextBoxにアタッチした後、フォーカスを強制することです。GotFocus

このような:

    public void PreparingCellForEdit(DataGridPreparingCellForEditEventArgs e)
    {
        var textBox = e.EditingElement as TextBox;
        if (textBox != null)
        {
            textBox.GotFocus += (s, e2) => textBox.SelectAll();
            textBox.Focus();
        }
    }
于 2011-07-03T13:43:23.277 に答える