2010-04-14 93 views
7

我试图让DataGridTemplateColumn以相同行为的TextColumn如何在DataGridTemplateColumn中的编辑文本框获得焦点时选择所有文本?

  • 当细胞进入编辑模式(按F2),用户可以立即开始键入新值
  • 默认情况下,现有文本内容被选中 - 以便您可以轻松设置新值

获得第一个完成;但是选择所有文本不起作用。正如许多帖子所提到的,尝试连接到GotFocus事件并选择代码隐藏中的所有文本。这适用于独立的文本框;但是对于一个TextBox,它是TemplateColumn的编辑控件,这不起作用。

任何想法? 代码示例:

<Window.Resources> 
      <Style x:Key="HighlightTextBoxStyle" TargetType="{x:Type TextBox}"> 
       <EventSetter Event="GotFocus" Handler="SelectAllText"/> 
       <EventSetter Event="GotMouseCapture" Handler="SelectAllText"/> 
       <Setter Property="Background" Value="AliceBlue"/> 
      </Style> 

      <DataTemplate x:Key="DefaultTitleTemplate"> 
       <TextBlock Text="{Binding Title}"/> 
      </DataTemplate> 
      <DataTemplate x:Key="EditTitleTemplate"> 
        <TextBox x:Name="Fox" 
         FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}" 
         Text="{Binding Path=Title, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
         Style="{StaticResource HighlightTextBoxStyle}"> 
        </TextBox> 
      </DataTemplate> 
     </Window.Resources> 
     <DockPanel> 
      <TextBox DockPanel.Dock="Top" x:Name="Test" Text="{Binding Path=(FocusManager.FocusedElement).Name, ElementName=MyWindow}" 
        Style="{StaticResource HighlightTextBoxStyle}"/> 
      <toolkit:DataGrid ItemsSource="{Binding Items}" AutoGenerateColumns="False"> 
       <toolkit:DataGrid.Columns> 
        <toolkit:DataGridTemplateColumn Header="Templated Title" 
         CellTemplate="{StaticResource DefaultTitleTemplate}" 
         CellEditingTemplate="{StaticResource EditTitleTemplate}" /> 

        <toolkit:DataGridTextColumn Header="Title" Binding="{Binding Path=Title}" /> 
       </toolkit:DataGrid.Columns> 
      </toolkit:DataGrid> 
     </DockPanel> 
+0

据我可以告诉这个问题仍然没有解决。 – Dabblernl 2011-04-25 22:30:56

+0

@Dabblernl - 尝试如果以下管道胶带修补程序的作品。 – Gishu 2011-04-26 06:51:46

回答

6

错过的更新与答案的帖子...

这个问题似乎是一个自定义数据网格列(又名一个DataGridTemplateColumn)电网无法知道的方式编辑控件的确切类型(通过DataTemplate指定,可以是任何东西)。对于DataGridTextColumn,编辑控件类型是已知的,因此网格可以找到它并在其中调用SelectAll()。

因此,要实现TemplateColumn的最终目标,您需要提供一个辅助。我忘记了我是如何第一次解决这个问题的......但这里是我搜索的东西 - 今天调整了出来。 使用覆盖PrepareCellForEdit方法的方式创建TemplateColumn的自定义派生,如下所示(使用精确编辑控件交换文本框)。

public class MyCustomDataColumn : DataGridTemplateColumn 
    { 
     protected override object PrepareCellForEdit(FrameworkElement editingElement, RoutedEventArgs editingEventArgs) 
     { 
      var contentPresenter = editingElement as ContentPresenter; 

      var editingControl = FindVisualChild<TextBox>(contentPresenter); 
      if (editingControl == null) 
       return null; 

      editingControl.SelectAll(); 
      return null; 
     } 

     private static childItem FindVisualChild<childItem>(DependencyObject obj) 
    } 

这是an implementation for FindVisualChild

XAML:代码为恼人的不一致

<WPFTestBed:MyCustomDataColumn Header="CustomColumn" 
        CellTemplate="{StaticResource DefaultTitleTemplate}" 
        CellEditingTemplate="{StaticResource EditTitleTemplate}"/> 
</DataGrid.Columns> 

地块。

+0

对不起,为奖励你太晚了。非常感谢! – Dabblernl 2011-05-03 05:51:06

+0

@Dabblernl - 在我遇到Mr.Skeet之后,不再追求代表ñ朋友:)只是乐于提供帮助。 – Gishu 2011-05-03 08:07:26

+0

在我的情况下,我还必须将焦点设置到编辑文本框 'editingControl.Focus();' 'editingControl.SelectAll();' – 2013-07-18 14:16:12

0

我知道这是晚了,但我采取了不同的方法,并创造性地扩展了TextBox类。我真的不喜欢使用布尔值来检查文本是否已经定义,但问题是选择事件在从绑定中设置文本之前都会触发,所以SelectAll()没有任何选择!这个类可能仅用作DataGridTemplateColumn之类的编辑模板。我发现这个问题的每一个解决方案都非常黑客,所以我不觉得这件事太糟糕了...... :)

class AutoSelectTextBox : TextBox 
{ 
    private bool _autoSelectAll= true; 

    protected override void OnInitialized(EventArgs e) 
    { 
     // This will cause the cursor to enter the text box ready to 
     // type even when there is no content. 
     Focus(); 
     base.OnInitialized(e); 
    } 

    protected override OnKeyDown(System.Windows.Input.KeyEventArgs e) 
    { 
     // This is here to handle the case of an empty text box. If 
     // omitted then the first character would be auto selected when 
     // the user starts typing. 
     _autoSelectAll = false; 
     base.OnKeyDown(e); 
    } 


    protected override void OnTextChanged(TextChangedEventArgs e) 
    { 
     if (_autoSelectAll) 
     { 
      SelectAll(); 
      Focus(); 
      _autoSelectAll= false; 
     } 
     base.OnTextChanged(e); 
    } 
} 
相关问题