编辑TextBox时默认选择的文本

时间:2015-07-27 19:52:00

标签: c# wpf text focus edit

基本上,我正在尝试设置编辑模式的行为,以便在进入编辑模式时选择包含文本。我正在尝试修改Name的属性TreeView.Item

我的研究显示我应该使用类似TextBlock.Focus()TextBlock.SelectAll()的内容,但我不知道如何做到这一点,因为我正在使用此方法进行编辑模式:

http://www.codeproject.com/Articles/72544/Editable-Text-Block-in-WPF?msg=5098686#xx5098686xx

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

添加SelectAll(),如下所示:

public EditableTextBlockAdorner(EditableTextBlock adornedElement)
    : base(adornedElement)
{
    _collection = new VisualCollection(this);
    _textBox = new TextBox();
    _textBlock = adornedElement;
    Binding binding = new Binding("Text") {Source = adornedElement};
    _textBox.SetBinding(TextBox.TextProperty, binding);
    _textBox.SelectAll();
    _textBox.AcceptsReturn = true;
    _textBox.MaxLength = adornedElement.MaxLength;
    _textBox.KeyUp += _textBox_KeyUp;
    _collection.Add(_textBox);
}
相关问题