gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 17:41:16 27 4
gpt4 key购买 nike

这个问题在这里已经有了答案:




9年前关闭。




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



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

最佳答案

网络

<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;
}

关于wpf - 如何确定 WPF DataGrid 是否处于编辑模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3279502/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com