10

我试图让 DataGridTemplateColumn 的行为与 TextColumn 相同

  • 当单元格进入编辑模式(按 F2)时,用户可以立即开始输入新值
  • 默认情况下,选择现有文本内容 - 以便您可以轻松设置新值

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

有任何想法吗?代码示例:

<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>
4

2 回答 2

8

错过了用答案更新帖子...

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

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

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) 
    }

这是FindVisualChild 的实现

XAML:

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

大量代码用于令人讨厌的不一致。

于 2011-04-26T06:50:55.767 回答
0

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

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);
    }
}
于 2015-02-04T18:10:11.657 回答