C# datagrid单元格获得焦点时自动编辑WPF datagrid内容
我有一个WPF中的datagrid,带有一个DataGridTextColumn和一个DataGridTemplateColumnC# datagrid单元格获得焦点时自动编辑WPF datagrid内容,c#,wpf,xaml,datagrid,C#,Wpf,Xaml,Datagrid,我有一个WPF中的datagrid,带有一个DataGridTextColumn和一个DataGridTemplateColumn <DataGridTextColumn Width="4*" IsReadOnly="True" x:Name="dataGridColumnDescription" Header="Description" Binding="{Binding Description}"> </DataGridTextColumn> <DataGr
<DataGridTextColumn Width="4*" IsReadOnly="True" x:Name="dataGridColumnDescription"
Header="Description" Binding="{Binding Description}">
</DataGridTextColumn>
<DataGridTemplateColumn CellStyle="{StaticResource CellEditing}" IsReadOnly="False" Width="*" Header="Value"
CellEditingTemplateSelector="{StaticResource myCellEditingTemplateSelectorValue}"
CellTemplateSelector="{StaticResource myCellTemplateSelectorValue}">
</DataGridTemplateColumn>
如何使用该事件处理程序获取文本框?这似乎可行:
<DataGrid ItemsSource="{Binding Items}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}"></TextBox>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
我管理了它,这不是最好的解决方案,但它很有效。。。 当单元格获得焦点时,我将其设置为编辑模式
private void myDataGridMain_OnFocus(object sender, RoutedEventArgs e)
{
DataGridCell cell = sender as DataGridCell;
if (cell != null)
cell.IsEditing = true;
//var test = FindVisualChild<TextBlock>(cell);
}
private void myDataGridMain\u OnFocus(对象发送方,RoutedEventTargets e)
{
DataGridCell=发送方为DataGridCell;
如果(单元格!=null)
cell.IsEditing=true;
//var测试=FindVisualChild(单元);
}
在按下键时,我搜索视觉儿童并给出焦点
private void myDataGridMain_KeyDown(object sender, KeyEventArgs e)
{
DataGridCell cell = sender as DataGridCell;
if (e.Key == Key.Enter)
{ //give cell the focus
cell.Focus();
}
else
{
if ((cell != null))
{
TextBox textbox = FindVisualChild<TextBox>(cell);
if (textbox != null)
{ //TextBox has benn found
if ((textbox as TextBox).IsFocused == false)
{
(textbox as TextBox).SelectAll();
}
(textbox as TextBox).Focus();
}
CheckBox chkbox = FindVisualChild<CheckBox>(cell);
if (chkbox != null)
{ //Checkbox has been found
(chkbox as CheckBox).Focus();
}
ComboBox combbox = FindVisualChild<ComboBox>(cell);
if (combbox != null)
{ //ComboBox has been found
(combbox as ComboBox).Focus();
}
}
}
}
private void myDataGridMain\u KeyDown(对象发送方,KeyEventArgs e)
{
DataGridCell=发送方为DataGridCell;
如果(e.Key==Key.Enter)
{//将焦点放在单元格上
cell.Focus();
}
其他的
{
如果((单元格!=null))
{
TextBox TextBox=FindVisualChild(单元格);
如果(文本框!=null)
{//TextBox已找到
if((textbox作为textbox).IsFocused==false)
{
(文本框作为文本框)。选择All();
}
(textbox作为textbox.Focus();
}
复选框chkbox=FindVisualChild(单元格);
if(chkbox!=null)
{//已找到复选框
(chkbox作为复选框)。焦点();
}
ComboBox ComboBox=FindVisualChild(单元格);
if(combbox!=null)
{//已找到组合框
(组合框作为组合框)。焦点();
}
}
}
}
找到视觉儿童
public static T FindVisualChild<T>(DependencyObject obj) where T : DependencyObject
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(obj, i);
if (child != null && child is T)
return (T)child;
else
{
T childOfChild = FindVisualChild<T>(child);
if (childOfChild != null)
return childOfChild;
}
}
return null;
}
public static T FindVisualChild(DependencyObject obj),其中T:DependencyObject
{
for(int i=0;i
这种方法对我很有效。它使用这样一个事实,即当编辑开始时,DataGrid
将始终创建模板的新实例:
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox Text="{Binding MyProperty}"
Loaded="TextBox_Loaded"></TextBox>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
另外,它还可以选择单元格中的所有文本。无论您如何进入编辑模式(双击、单击、按F2键),它都应该工作。创建从datagrid控件派生的新控件的简单答案
using System.Windows.Controls;
public class CustomDataGrid : DataGrid
{
protected override void OnSelectedCellsChanged(SelectedCellsChangedEventArgs e)
{
//to make sure cell is selected
var cells = e.AddedCells.FirstOrDefault();
if (cells != null)
{
this.BeginEdit();
}
base.OnSelectedCellsChanged(e);
}
}
建议对我来说非常有效,但我会使用OnCurrentCellChanged
,因为OnSelectedCellsChanged
在SelectionUnit
为CellOrRowHeader
时将不起作用。在后一种情况下,BeginEdit()
仅在选择移动到另一行的单元格时才会触发。向左或向右移动根本不会触发事件
此外,可能建议在触发BeginEdit()
之前向自定义控件添加DependencyProperty并对其进行检查,以防止这种行为(如其他数据网格,如XCeed所做的那样)。但这不是一个批评家——只是我通常做的事情
protected override void OnCurrentCellChanged(EventArgs e)
{
// Make sure a cell is selected and only enter edit mode
// if this is the desired behavior
if (CurrentCell != null && EditTrigger == EditTriggers.CellsCurrent)
{
this.BeginEdit();
}
base.OnCurrentCellChanged(e);
}
谢谢你的回答。与模板选择器组合使用时,这不起作用:(谢谢,您是一个很棒的向导!我有一个复杂的带有组合框和文本框的数据网格。此示例解决了我所有单元格类型的问题。@HamedR great:)当您希望网格始终可编辑时,为什么要编辑模板?只需添加包含文本框的单元格模板,并改变文本框本身的样式,这样您就不必编写焦点stuff@WPFKK这是行不通的,因为datagrid不会切换到“编辑模式”,因此输入值不会绑定到源。
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox Text="{Binding MyProperty}"
Loaded="TextBox_Loaded"></TextBox>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
private void TextBox_Loaded(object sender, RoutedEventArgs e)
{
((TextBox)sender).Focus();
((TextBox)sender).SelectAll();
}
using System.Windows.Controls;
public class CustomDataGrid : DataGrid
{
protected override void OnSelectedCellsChanged(SelectedCellsChangedEventArgs e)
{
//to make sure cell is selected
var cells = e.AddedCells.FirstOrDefault();
if (cells != null)
{
this.BeginEdit();
}
base.OnSelectedCellsChanged(e);
}
}
protected override void OnCurrentCellChanged(EventArgs e)
{
// Make sure a cell is selected and only enter edit mode
// if this is the desired behavior
if (CurrentCell != null && EditTrigger == EditTriggers.CellsCurrent)
{
this.BeginEdit();
}
base.OnCurrentCellChanged(e);
}