Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#WPF数据网格:从SelectionChanged事件获取值_C#_Wpf_Datagrid - Fatal编程技术网

C#WPF数据网格:从SelectionChanged事件获取值

C#WPF数据网格:从SelectionChanged事件获取值,c#,wpf,datagrid,C#,Wpf,Datagrid,这一整天都快把我逼疯了。我只需要datagrid中所选行的值,它在VB.net中工作,我对c#有点陌生,我无法让它工作 在VB中,我的工作代码是: Private Sub dg_qc_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Dim TempListItem As QCProperties = CType(sende

这一整天都快把我逼疯了。我只需要datagrid中所选行的值,它在VB.net中工作,我对c#有点陌生,我无法让它工作

在VB中,我的工作代码是:

Private Sub dg_qc_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs)

        Dim TempListItem As QCProperties = CType(sender, DataGrid).SelectedItem

        Dim temp1 As String = TempListItem.PartNumber

End Sub
在C#中,我有:

我遇到错误“无法将类型“object”隐式转换为“MyProperties类”(是否缺少强制转换?)

我已经在网上搜索过了,但我没有运气,我知道这是一个简单的解决办法。请帮助

谢谢,
Chelsey

您只需将SelectedItem转换为正确的类型:

dgProperties tempItems = ((DataGrid)sender).SelectedItem as dgProperties;

请注意,在访问modelRev和date等属性之前,您应该检查以确保tempItems!=null。

为什么此标记为WPF这非常适合创建自己模型的用户。谢谢。
dgProperties tempItems = ((DataGrid)sender).SelectedItem as dgProperties;