如何确定 WPF DataGrid 是否处于编辑模式?

发布于 2024-09-10 14:10:00 字数 261 浏览 7 评论 0原文

可能的重复:
用于检查某个单元格是否为DataGrid 当前正在编辑

有没有办法确定 WPF DataGrid 是否处于编辑模式/当前正在编辑哪一行?

Possible Duplicate:
Code to check if a cell of a DataGrid is currently edited

Is there a way to determine whether a WPF DataGrid is in edit mode / which row is currently edited?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鹿! 2024-09-17 14:10:00

VB.NET

<Extension>
Public Function GetContainerFromIndex(Of TContainer As DependencyObject) _
    (ByVal itemsControl As ItemsControl, ByVal index As Integer) As TContainer
  Return DirectCast(
    itemsControl.ItemContainerGenerator.ContainerFromIndex(index), TContainer)
End Function

<Extension>
Public Function IsEditing(ByVal dataGrid As DataGrid) As Boolean
  Return dataGrid.GetEditingRow IsNot Nothing
End Function

<Extension>
Public Function GetEditingRow(ByVal dataGrid As DataGrid) As DataGridRow
  Dim sIndex = dataGrid.SelectedIndex
  If sIndex >= 0 Then
    Dim selected = dataGrid.GetContainerFromIndex(Of DataGridRow)(sIndex)
    If selected.IsEditing Then Return selected
  End If

  For i = 0 To dataGrid.Items.Count - 1
    If i = sIndex Then Continue For
    Dim item = dataGrid.GetContainerFromIndex(Of DataGridRow)(i)
    If item.IsEditing Then Return item
  Next

  Return Nothing
End Function

C#:

public static TContainer GetContainerFromIndex<TContainer>
  (this ItemsControl itemsControl, int index)
    where TContainer : DependencyObject
{
  return (TContainer)
    itemsControl.ItemContainerGenerator.ContainerFromIndex(index);
}

public static bool IsEditing(this DataGrid dataGrid)
{
  return dataGrid.GetEditingRow() != null;
}

public static DataGridRow GetEditingRow(this DataGrid dataGrid)
{
  var sIndex = dataGrid.SelectedIndex;
  if (sIndex >= 0)
  {
    var selected = dataGrid.GetContainerFromIndex<DataGridRow>(sIndex);
    if (selected.IsEditing) return selected;
  }

  for (int i = 0; i < dataGrid.Items.Count; i++)
  {
    if (i == sIndex) continue;
    var item = dataGrid.GetContainerFromIndex<DataGridRow>(i);
    if (item.IsEditing) return item;
  }

  return null;
}

VB.NET

<Extension>
Public Function GetContainerFromIndex(Of TContainer As DependencyObject) _
    (ByVal itemsControl As ItemsControl, ByVal index As Integer) As TContainer
  Return DirectCast(
    itemsControl.ItemContainerGenerator.ContainerFromIndex(index), TContainer)
End Function

<Extension>
Public Function IsEditing(ByVal dataGrid As DataGrid) As Boolean
  Return dataGrid.GetEditingRow IsNot Nothing
End Function

<Extension>
Public Function GetEditingRow(ByVal dataGrid As DataGrid) As DataGridRow
  Dim sIndex = dataGrid.SelectedIndex
  If sIndex >= 0 Then
    Dim selected = dataGrid.GetContainerFromIndex(Of DataGridRow)(sIndex)
    If selected.IsEditing Then Return selected
  End If

  For i = 0 To dataGrid.Items.Count - 1
    If i = sIndex Then Continue For
    Dim item = dataGrid.GetContainerFromIndex(Of DataGridRow)(i)
    If item.IsEditing Then Return item
  Next

  Return Nothing
End Function

C#:

public static TContainer GetContainerFromIndex<TContainer>
  (this ItemsControl itemsControl, int index)
    where TContainer : DependencyObject
{
  return (TContainer)
    itemsControl.ItemContainerGenerator.ContainerFromIndex(index);
}

public static bool IsEditing(this DataGrid dataGrid)
{
  return dataGrid.GetEditingRow() != null;
}

public static DataGridRow GetEditingRow(this DataGrid dataGrid)
{
  var sIndex = dataGrid.SelectedIndex;
  if (sIndex >= 0)
  {
    var selected = dataGrid.GetContainerFromIndex<DataGridRow>(sIndex);
    if (selected.IsEditing) return selected;
  }

  for (int i = 0; i < dataGrid.Items.Count; i++)
  {
    if (i == sIndex) continue;
    var item = dataGrid.GetContainerFromIndex<DataGridRow>(i);
    if (item.IsEditing) return item;
  }

  return null;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文