wpf 数据网格文本列单击编辑模式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19254004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Data grid text column single-click edit mode
提问by kformeck
I have a DataGrid in XAML where every column is a text column. I define a column template for every column. I want to be able to click once on a cell and be in edit mode rather than having to double click on it. I have followed this: http://wpf.codeplex.com/wikipage?title=Single-Click%20Editingand I have not had any success. Right now, the sample code shown below will only bring the cell into focus in a single click, it will not actually put me in edit mode. Any help will be greatly appreciated!
我在 XAML 中有一个 DataGrid,其中每一列都是一个文本列。我为每一列定义了一个列模板。我希望能够在单元格上单击一次并处于编辑模式,而不必双击它。我遵循了这个:http: //wpf.codeplex.com/wikipage?title=Single-Click%20Editing并且我没有取得任何成功。现在,下面显示的示例代码只会通过单击使单元格成为焦点,它实际上不会让我进入编辑模式。任何帮助将不胜感激!
Here is the data grid:
这是数据网格:
<DataGrid
DockPanel.Dock="Bottom"
x:Name="grdData"
FontFamily="Verdana"
Height="200"
AutoGenerateColumns="False"
RowHeight="22"
CanUserAddRows="True"
CanUserDeleteRows="True"
CanUserReorderColumns="False"
CanUserResizeColumns="True"
CanUserResizeRows="True"
CanUserSortColumns="True"
ItemsSource="{Binding GridData}"
SelectionUnit="CellOrRowHeader"
SelectionMode="Extended">
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridCell}">
<EventSetter
Event="PreviewMouseLeftButtonDown"
Handler="DataGridCell_PreviewMouseLeftButtonDown"/>
</Style>
</DataGrid.Resources>
Here is a sample column template that I define:
这是我定义的示例列模板:
<DataGridTemplateColumn Width="60">
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<TextBlock
Style="{StaticResource tbkStyleGridHeader}"
TextWrapping="Wrap"
Text="GelPak Location"/>
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock
Style="{StaticResource tbkStyleGridCell}"
TextWrapping="Wrap"
Text="{Binding GelPakLocation}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox
Text="{Binding GelPakLocation, Mode=TwoWay}"/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
And here is the code-behind:
这是代码隐藏:
private void DataGridCell_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
DataGridCell cell = (DataGridCell) sender;
if (cell != null && !cell.IsEditing && !cell.IsReadOnly)
{
if (!cell.IsFocused)
{
cell.Focus();
}
DataGrid dataGrid = FindVisualParent<DataGrid>(cell);
if (dataGrid != null)
{
if (dataGrid.SelectionUnit != DataGridSelectionUnit.FullRow)
{
if (!cell.IsSelected)
{
cell.IsSelected = true;
cell.IsEditing = true;
}
}
else
{
DataGridRow row = FindVisualParent<DataGridRow>(cell);
if (row != null && !row.IsSelected)
{
row.IsSelected = true;
}
}
}
}
}
static T FindVisualParent<T>(UIElement element) where T : UIElement
{
UIElement parent = element;
while (parent != null)
{
T correctlyTyped = parent as T;
if (correctlyTyped != null)
{
return correctlyTyped;
}
parent = VisualTreeHelper.GetParent(parent) as UIElement;
}
return null;
}
回答by kformeck
I solved it:
我解决了:
Following this tutorial: http://wpf.codeplex.com/wikipage?title=Single-Click%20Editing
按照本教程:http: //wpf.codeplex.com/wikipage?title =Single-Click% 20Editing
I just changed my DataGridTemplateColumns to DataGridTextBoxColumns. In doing this, I had to get rid of the CellTemplate and CellEditingTemplate elements.
我只是将我的 DataGridTemplateColumns 更改为 DataGridTextBoxColumns。在这样做时,我必须摆脱 CellTemplate 和 CellEditingTemplate 元素。
Now my column template looks like this:
现在我的列模板如下所示:
<DataGridTextColumn
Width="60"
Binding="{Binding GelPakLocation}">
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<TextBlock
Style="{StaticResource tbkStyleGridHeader}"
TextWrapping="Wrap"
Text="GelPak Location"/>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
Doing this also allowed me to delete this line in the PreviewMouseLeftButtonDown event handler:
这样做还允许我删除 PreviewMouseLeftButtonDown 事件处理程序中的这一行:
cell.IsEditing = true;
It seems like this line didn't really do anything anyways.
无论如何,这条线似乎并没有真正做任何事情。
回答by JFTxJ
I would recommend that you use a TextBox in the actual CellTemplate and remove the CellEditingTemplate alltogether. Then you can use a focus trigger on the textboxes to (for example) change the background color and readonly property of the text box as it gains focus...
我建议您在实际的 CellTemplate 中使用 TextBox 并将 CellEditingTemplate 全部删除。然后您可以在文本框上使用焦点触发器来(例如)在文本框获得焦点时更改背景颜色和只读属性...

