Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.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
Datagrid行选择事件,WPF_Wpf_Events_Wpfdatagrid - Fatal编程技术网

Datagrid行选择事件,WPF

Datagrid行选择事件,WPF,wpf,events,wpfdatagrid,Wpf,Events,Wpfdatagrid,我做了一个简单的数据绑定到datagrid。现在,我想在datagrid中单击行时获取相关的行数据(整行数据)。由于没有行选择事件,我是否需要使用mouseclickevent?我是这样做的。其他人可能有更简单的方法!Mine处理mediaplayer的PlayEntries类的可观察集合。希望这能有所帮助 private void PlayList_OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e) {

我做了一个简单的数据绑定到datagrid。现在,我想在datagrid中单击行时获取相关的行数据(整行数据)。由于没有行选择事件,我是否需要使用mouseclickevent?

我是这样做的。其他人可能有更简单的方法!Mine处理mediaplayer的PlayEntries类的可观察集合。希望这能有所帮助

    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 
}