WPF DataGrid中的新项目占位符消失
我的DataGrid中有一个无效的新项目行。我将焦点更改为另一个元素,它将清除此数据网格的备份集合。这会导致新项目占位符(datagrid底部的空行)消失WPF DataGrid中的新项目占位符消失,wpf,datagrid,Wpf,Datagrid,我的DataGrid中有一个无效的新项目行。我将焦点更改为另一个元素,它将清除此数据网格的备份集合。这会导致新项目占位符(datagrid底部的空行)消失 如何使其重新出现?如果您正在清除集合,使DataGrid完全不显示行,那么听起来您面临的问题与此处讨论的相同: 基本上,问题在于DataGrid不知道绑定到什么对象类型,因此无法为新项目占位符生成列(例如,它不知道是否为人名、订单的交付日期或宠物的头发颜色等提供空白单元格)。 上面链接的讨论中的一个答案是添加一个虚拟对象,然后将其删除,这对
如何使其重新出现?如果您正在清除集合,使DataGrid完全不显示行,那么听起来您面临的问题与此处讨论的相同: 基本上,问题在于DataGrid不知道绑定到什么对象类型,因此无法为新项目占位符生成列(例如,它不知道是否为人名、订单的交付日期或宠物的头发颜色等提供空白单元格)。 上面链接的讨论中的一个答案是添加一个虚拟对象,然后将其删除,这对我来说很有效。我无法使“删除项目/重新插入项目”修复程序对我的应用程序起作用。在解决此问题时,我注意到DataGrid的内置delete命令将1)删除选定的行,即使对于绑定了ItemsSource的DataGrid也是如此;2)如果在编辑模式下对行触发delete命令,则不会使NewItemPlaceHolder行消失 这是我的应用程序的DataGrid,它有一个DataGridTemplateColumn,其中包含一个使用buil in delete命令的按钮。 注意:必须选择该行才能启用该按钮,然后单击该按钮将删除该行。不过,我相信您可以将DataGrid的OnCanExecuteDelete子类化并修改此行为
<local:MyDataGrid
ItemsSource="{Binding Companies}"
AutoGenerateColumns="False"
RowHeaderWidth="20"
x:Name="dg">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Delete">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="X"
Command="DataGrid.DeleteCommand"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
</DataGrid.Columns>
作为奖励,这里有一个子类DataGrid,它给了我和你(或曾经——这是一篇老文章)面临的同样的问题。它包括代码I,允许对任何DataGridCell进行一键编辑
public class MyDataGrid : DataGrid
{
public MyDataGrid()
{
this.GotFocus += DataGrid_CellGotFocus;
}
protected override void OnExecutedDelete(ExecutedRoutedEventArgs e)
{
// Temporarily remove the GotFocus eventhandler to prevent
// the datagrid from firing BeginEdit after a row is deleted.
this.GotFocus -= DataGrid_CellGotFocus;
base.OnExecutedDelete(e);
this.GotFocus += DataGrid_CellGotFocus;
}
private void DataGrid_CellGotFocus(object sender, RoutedEventArgs e)
{
// Lookup for the source to be DataGridCell
if (e.OriginalSource.GetType() == typeof(DataGridCell))
{
// Starts the Edit on the row;
DataGrid grd = (DataGrid)sender;
grd.BeginEdit(e);
//Control control = e.OriginalSource as DataGridCell;
Control control = GetFirstChildByType<Control>(e.OriginalSource as DataGridCell);
if (control != null)
{
control.Focus();
}
}
}
/// <summary>
/// Returns the first visual tree child item matching T.
/// </summary>
/// <param name="prop"></param>
/// <returns></returns>
private T GetFirstChildByType<T>(DependencyObject prop) where T : DependencyObject
{
// Begin a loop thru all visual tree items for the DependencyObject arg.
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(prop); i++)
{
// Loop to the next visual tree item if the current item is null.
DependencyObject child = VisualTreeHelper.GetChild((prop), i) as DependencyObject;
if (child == null)
continue;
// Try casting the current item to T. If the cast works, return the visual tree
// item as T.
T castedProp = child as T;
if (castedProp != null)
return castedProp;
// If this code line is reached, the cast failed. Recursively call this method.
castedProp = GetFirstChildByType<T>(child);
if (castedProp != null)
return castedProp;
}
// If this code line is reached, no visual tree items in prop match T. Return null.
return null;
}
}
公共类MyDataGrid:DataGrid
{
公共MyDataGrid()
{
this.GotFocus+=DataGrid_CellGotFocus;
}
受保护的覆盖无效OnExecutedDelete(已执行路由目标e)
{
//暂时删除GotFocus事件处理程序以防止
//数据网格在删除行后触发BeginEdit。
this.GotFocus-=DataGrid_CellGotFocus;
碱基。一个被切割的小裂片(e);
this.GotFocus+=DataGrid_CellGotFocus;
}
私有void DataGrid_CellGotFocus(对象发送方,RoutedEventTargets e)
{
//查找要成为DataGridCell的源
if(e.OriginalSource.GetType()==typeof(DataGridCell))
{
//开始对行进行编辑;
DataGrid grd=(DataGrid)发送方;
grd.BeginEdit(e);
//Control=e.OriginalSource作为DataGridCell;
Control=GetFirstChildByType(例如,原始源为DataGridCell);
if(控件!=null)
{
control.Focus();
}
}
}
///
///返回与T匹配的第一个可视树子项。
///
///
///
私有T GetFirstChildByType(DependencyObject属性),其中T:DependencyObject
{
//开始循环,遍历DependencyObject arg的所有可视树项。
for(int i=0;i