Datagrid行选择事件,WPF
我做了一个简单的数据绑定到datagrid。现在,我想在datagrid中单击行时获取相关的行数据(整行数据)。由于没有行选择事件,我是否需要使用mouseclickevent?我是这样做的。其他人可能有更简单的方法!Mine处理mediaplayer的PlayEntries类的可观察集合。希望这能有所帮助Datagrid行选择事件,WPF,wpf,events,wpfdatagrid,Wpf,Events,Wpfdatagrid,我做了一个简单的数据绑定到datagrid。现在,我想在datagrid中单击行时获取相关的行数据(整行数据)。由于没有行选择事件,我是否需要使用mouseclickevent?我是这样做的。其他人可能有更简单的方法!Mine处理mediaplayer的PlayEntries类的可观察集合。希望这能有所帮助 private void PlayList_OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e) {
private void PlayList_OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
DependencyObject dep = (DependencyObject)e.OriginalSource;
// iteratively traverse the visual tree
while ((dep != null) && !(dep is DataGridCell) && !(dep is DataGridColumnHeader))
{
dep = VisualTreeHelper.GetParent(dep);
}
if (dep == null)
return;
if (dep is DataGridColumnHeader)
{
DataGridColumnHeader columnHeader = dep as DataGridColumnHeader;
// do something
}
if (dep is DataGridCell)
{
DataGridCell cell = dep as DataGridCell;
// navigate further up the tree
while ((dep != null) && !(dep is DataGridRow))
{
dep = VisualTreeHelper.GetParent(dep);
}
DataGridRow row = dep as DataGridRow;
var ple = (PlayListEntry)row.Item;
// From here you have access to all of the row.
// Each column is suitable bound.
// I can post the xaml if you are not sure.
//object value = ExtractBoundValue(row, cell); //4
//int columnIndex = cell.Column.DisplayIndex;
//int rowIndex = FindRowIndex(row);
//var s = string.Format("Cell clicked [{0}, {1}] = {2}",rowIndex, columnIndex, value.ToString());
}
}
private void dataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ProductItem productItem = (ProductItem)dataGrid.SelectedItem; //Datagrid bound with ProductItem
}