如果我点击空白 space,C# DataGrid ContextMenuOpening 错误
C# DataGrid ContextMenuOpening error if I click on empty space
首先,我正在学习,所以请不要难为我。
我有一个从数据库中获取数据的 DataGrid,我想添加包含第一列 (Id) 的 ContextMenu,以便我可以对数据库使用删除操作。
我搜索了一下,最后得到了这个代码:
用于添加上下文菜单XML
<DataGrid.ContextMenu>
<ContextMenu>
<MenuItem Header="delete" Click="BtnFactorDell">
<MenuItem.Icon>
<Image Width="12" Height="12" Source="img/delete.png"/>
</MenuItem.Icon>
</ContextMenu>
</DataGrid.ContextMenu>
为了选择我的 ID 第一列,我使用了这个:
string factorselectedid;
private void FactorGrid_ContextMenuOpening(object sender, ContextMenuEventArgs e)
{
object item = FactorGrid.SelectedItem;
factorselectedid = (FactorGrid.SelectedCells[0].Column.GetCellContent(item) as TextBlock).Text;
}
现在我有 1 个问题和 1 个问题,第一个问题是最后那个 TextBlock 是什么?因为它是我通过搜索找到的代码中的 TexBox 并且我遇到了一些错误所以我随机将它更改为 TextBlock 并且它工作正常!现在我对这是什么感到困惑。
关于我的问题是,当我在 DataGridview 上单击空 space 时,出现错误:
System.ArgumentOutOfRangeException: 'Specified argument was out of the range of valid values.
Parameter name: index'
上线
factorselectedid = (FactorGrid.SelectedCells[0].Column.GetCellContent(item) as TextBlock).Text;
如果单击 Datagrid 的非空文件 space,我该怎么做才能工作?
谢谢。
what is that TextBlock at the end?
GetCellContent(item)
returns 一个 FrameworkElement,但是 FrameworkElement 没有 Text
属性。在您的情况下,返回的是一个 TextBlock
的(派生),其中 是 一个 FrameworkElement
(TextBlock 派生自 FrameworkElement),但是要访问 Text
属性 你必须先将返回的 FrameworkElement
转换为 TextBlock
您可能已经看到了强制转换为 TextBox
的示例;如果被投射的东西 实际上是 TextBox
- 在你的情况下它是 而不是 TextBox
但这些例子会起作用它是一个 TextBlock
(或从 TextBlock
派生的其他东西),因此转换为 TextBlock
可以正常工作。
System.ArgumentOutOfRangeException: 'Specified argument was out of the range of valid values. Parameter name: index'
您只是直接尝试访问 SelectedCells
中的第一个单元格 ([0]
),而根本不知道是否有任何选定的单元格;如果没有选中的单元格,你就会遇到问题
改为考虑在尝试访问之前检查是否至少有 1 个单元格是:
if(FactorGrid.SelectedCells.Count > 0)
factorselectedid = (FactorGrid.SelectedCells[0].Column.GetCellContent(item) as TextBlock)?.Text;
else
e.Handled = true; //suppress the context menu from opening at all
我还在 .Text
之前添加了 ?
- 如果它 不是 TextBlock,它将防止崩溃,但这意味着factorselectedid
最终变成 null
。任何使用 factorselectedid
的代码都应该检查它是否为 null first
首先,我正在学习,所以请不要难为我。 我有一个从数据库中获取数据的 DataGrid,我想添加包含第一列 (Id) 的 ContextMenu,以便我可以对数据库使用删除操作。
我搜索了一下,最后得到了这个代码:
用于添加上下文菜单XML
<DataGrid.ContextMenu>
<ContextMenu>
<MenuItem Header="delete" Click="BtnFactorDell">
<MenuItem.Icon>
<Image Width="12" Height="12" Source="img/delete.png"/>
</MenuItem.Icon>
</ContextMenu>
</DataGrid.ContextMenu>
为了选择我的 ID 第一列,我使用了这个:
string factorselectedid;
private void FactorGrid_ContextMenuOpening(object sender, ContextMenuEventArgs e)
{
object item = FactorGrid.SelectedItem;
factorselectedid = (FactorGrid.SelectedCells[0].Column.GetCellContent(item) as TextBlock).Text;
}
现在我有 1 个问题和 1 个问题,第一个问题是最后那个 TextBlock 是什么?因为它是我通过搜索找到的代码中的 TexBox 并且我遇到了一些错误所以我随机将它更改为 TextBlock 并且它工作正常!现在我对这是什么感到困惑。
关于我的问题是,当我在 DataGridview 上单击空 space 时,出现错误:
System.ArgumentOutOfRangeException: 'Specified argument was out of the range of valid values. Parameter name: index'
上线
factorselectedid = (FactorGrid.SelectedCells[0].Column.GetCellContent(item) as TextBlock).Text;
如果单击 Datagrid 的非空文件 space,我该怎么做才能工作? 谢谢。
what is that TextBlock at the end?
GetCellContent(item)
returns 一个 FrameworkElement,但是 FrameworkElement 没有 Text
属性。在您的情况下,返回的是一个 TextBlock
的(派生),其中 是 一个 FrameworkElement
(TextBlock 派生自 FrameworkElement),但是要访问 Text
属性 你必须先将返回的 FrameworkElement
转换为 TextBlock
您可能已经看到了强制转换为 TextBox
的示例;如果被投射的东西 实际上是 TextBox
- 在你的情况下它是 而不是 TextBox
但这些例子会起作用它是一个 TextBlock
(或从 TextBlock
派生的其他东西),因此转换为 TextBlock
可以正常工作。
System.ArgumentOutOfRangeException: 'Specified argument was out of the range of valid values. Parameter name: index'
您只是直接尝试访问 SelectedCells
中的第一个单元格 ([0]
),而根本不知道是否有任何选定的单元格;如果没有选中的单元格,你就会遇到问题
改为考虑在尝试访问之前检查是否至少有 1 个单元格是:
if(FactorGrid.SelectedCells.Count > 0)
factorselectedid = (FactorGrid.SelectedCells[0].Column.GetCellContent(item) as TextBlock)?.Text;
else
e.Handled = true; //suppress the context menu from opening at all
我还在 .Text
之前添加了 ?
- 如果它 不是 TextBlock,它将防止崩溃,但这意味着factorselectedid
最终变成 null
。任何使用 factorselectedid
的代码都应该检查它是否为 null first