【问题标题】:Select all Silverlight DataGrid cell text on cell entry在单元格条目上选择所有 Silverlight DataGrid 单元格文本
【发布时间】:2011-06-07 12:15:59
【问题描述】:

我在 SL4 中有一个带有简单 DataGridTextColumn 列的 DataGrid。

一旦单元格更改为可编辑的文本框,我尝试了多种不同的方法来选择 DataGridCell 中的所有文本。

下面的代码是我最后一次尝试。

在调试中检查 TextBox 显示 SelectedText 属性等于 Text 属性。所以问题不在于文本框。稍后似乎有些东西正在取消选择文本。

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

任何想法如何保持文本被选中并将带有所选文本的文本框显示给用户?

附:我正在使用 Cliburn.Micro 附加 PreparingCellForEdit 事件。

【问题讨论】:

    标签: silverlight datagrid textbox selectall


    【解决方案1】:

    对我来说更好的是:

    public void PreparingCellForEdit(DataGridPreparingCellForEditEventArgs e)
    {
        var textBox = e.EditingElement as TextBox;
        if (textBox != null)
        {
            textBox.Dispatcher.BeginInvoke(() => textBox.SelectAll());
        }
    }
    

    【讨论】:

      【解决方案2】:

      有些解决方案是在附加到GotFocus 事件后强制关注TextBox

      像这样:

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

      【讨论】:

        猜你喜欢
        • 2010-12-11
        • 1970-01-01
        • 2021-03-29
        • 1970-01-01
        • 2013-07-01
        • 2010-12-09
        • 2017-08-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多