Datagrid行选择事件,WPF
Posted
技术标签:
【中文标题】Datagrid行选择事件,WPF【英文标题】:Datagrid row selection event, WPF 【发布时间】:2014-03-18 22:22:54 【问题描述】:我对数据网格做了一个简单的数据绑定。现在我想在数据网格中单击行时获取相关的行数据(整行数据)。由于没有行选择事件,我是否需要使用 mouseclickevent?。
【问题讨论】:
【参考方案1】:private void dataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
ProductItem productItem = (ProductItem)dataGrid.SelectedItem; //Datagrid bound with ProductItem
【讨论】:
【参考方案2】:我是这样做的。其他人可能有更简单的方法!我的处理媒体播放器的 PlayListEntries 类的可观察集合。希望这可以帮助
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());
【讨论】:
以上是关于Datagrid行选择事件,WPF的主要内容,如果未能解决你的问题,请参考以下文章