我正在尝试让DataGridTemplateColumn的行为与TextColumn相同
完成了第一个;但是选择所有文本都不起作用。正如许多帖子所提到的,尝试挂钩到GotFocus事件并选择代码隐藏中的所有文本。这适用于独立的文本框;但是对于作为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 :(得分: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>
许多令人讨厌的不一致的代码。
答案 1 :(得分: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);
}
}