简体   繁体   English

在Silverlight DataGrid中获取行

[英]Get row in Silverlight DataGrid

I am trying to use code below but it is not working because of the ItemContainerGenerator 我正在尝试使用下面的代码,但是由于ItemContainerGenerator而无法使用

  var selectedRow = (DataGridRow)myGrid.ItemContainerGenerator.ContainerFromItem(myGrid.SelectedItem);

Is this only one solution? 这只是一个解决方案吗?

How to fix it? 如何解决?

You have to pick it from the visual tree , the DataGrid offers no convenient access. 您必须从visual tree选择它, DataGrid无法提供方便的访问。

var selectedRow = myGrid.GetVisualDescendants()
                        .OfType<DataGridRow>()
                        .Where( row => row.DataContext == myGrid.SelectedItem)
                        .SingleOrDefault();

I recommend you write an extension method for this, it will enhance the code's readability and you can reuse it easily. 我建议您为此编写一个扩展方法,它将增强代码的可读性,并且可以轻松地重用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM