none
如何在DataGrid的Hyperlink.Click的事件代码中获得单元格信息?以及如何设置DataGridHyperlinkColumn的超链接外观? RRS feed

  • 问题

  • 设置某一个列为DataGridHyperlinkColumn类型,并这设置Hyperlink.Click事件;

    如何在DataGrid的Hyperlink.Click的事件代码中获得单元格信息?包括所在列信息(得到所属的DataGridColumn对象)和行信息(绑定实体对象)这两项。

    如何设置DataGridHyperlinkColumn展示的超链接单元格的文章外观,包括鼠标悬停时和非鼠标悬停时和点击时的外观三种情况下的单元格外观?

    2019年9月22日 8:52

答案

  • Hi    轮回的齿轮,

    >>如何在DataGrid的Hyperlink.Click的事件代码中获得单元格信息?包括所在列信息(得到所属的DataGridColumn对象)和行信息(绑定实体对象)这两项。

    可以尝试下面的方法

     private void OnHyperlinkClick(object sender, RoutedEventArgs e)
            {
                //当前对象
                Hyperlink hp = (Hyperlink)e.OriginalSource;
                //绑定行 实体对象
                User filedatasource = hp.DataContext as User;
    
                var destination = hp.NavigateUri;
    
                MessageBox.Show(destination.ToString());
    
            }

    >> 如何设置DataGridHyperlinkColumn展示的超链接单元格的文章外观,包括鼠标悬停时和非鼠标悬停时和点击时的外观三种情况下的单元格外观?

    你可以尝试使用DataGridBoundColumn.ElementStyle 来设置样式。 并通过Trigger Property="IsMouseOver" 或者  <EventTrigger RoutedEvent="Hyperlink.Click"> 来设定对应的样式。

    Best regards

    Yong Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2019年9月23日 7:43
    版主
  • Hi   轮回的齿轮,

    >> 单元格所属的列对象(DataGridHyperlinkColumn或DataGridColumn)怎么得到?

    参考下面的方法:

        private void OnHyperlinkClick(object sender, RoutedEventArgs e)
            {
    
                //当前对象
                Hyperlink hp = (Hyperlink)e.OriginalSource;
                //绑定行 实体对象
                User filedatasource = hp.DataContext as User;
    
                DataGridRow row = (DataGridRow)dgUsers.ItemContainerGenerator.ContainerFromItem(filedatasource);
                if (row != null)
                {
                    DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row);
    
                    DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(0);
                    if (cell != null)
                    {
                       // 根据你自己的index 获取对应的DataGridCell。
                    }
                }
    
            }
            
            public static T GetVisualChild<T>(Visual parent) where T : Visual
            {
                T child = default(T);
                int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
                for (int i = 0; i < numVisuals; i++)
                {
                    Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
                    child = v as T;
                    if (child == null)
                    {
                        child = GetVisualChild<T>(v);
                    }
                    if (child != null)
                    {
                        break;
                    }
                }
                return child;
            }
    Best regards

    Yong Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2019年9月24日 2:47
    版主

全部回复

  • Hi    轮回的齿轮,

    >>如何在DataGrid的Hyperlink.Click的事件代码中获得单元格信息?包括所在列信息(得到所属的DataGridColumn对象)和行信息(绑定实体对象)这两项。

    可以尝试下面的方法

     private void OnHyperlinkClick(object sender, RoutedEventArgs e)
            {
                //当前对象
                Hyperlink hp = (Hyperlink)e.OriginalSource;
                //绑定行 实体对象
                User filedatasource = hp.DataContext as User;
    
                var destination = hp.NavigateUri;
    
                MessageBox.Show(destination.ToString());
    
            }

    >> 如何设置DataGridHyperlinkColumn展示的超链接单元格的文章外观,包括鼠标悬停时和非鼠标悬停时和点击时的外观三种情况下的单元格外观?

    你可以尝试使用DataGridBoundColumn.ElementStyle 来设置样式。 并通过Trigger Property="IsMouseOver" 或者  <EventTrigger RoutedEvent="Hyperlink.Click"> 来设定对应的样式。

    Best regards

    Yong Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2019年9月23日 7:43
    版主
  • Hi    轮回的齿轮,

    >>如何在DataGrid的Hyperlink.Click的事件代码中获得单元格信息?包括所在列信息(得到所属的DataGridColumn对象)和行信息(绑定实体对象)这两项。

    可以尝试下面的方法

     private void OnHyperlinkClick(object sender, RoutedEventArgs e)
            {
                //当前对象
                Hyperlink hp = (Hyperlink)e.OriginalSource;
                //绑定行 实体对象
                User filedatasource = hp.DataContext as User;
    
                var destination = hp.NavigateUri;
    
                MessageBox.Show(destination.ToString());
    
            }

    >> 如何设置DataGridHyperlinkColumn展示的超链接单元格的文章外观,包括鼠标悬停时和非鼠标悬停时和点击时的外观三种情况下的单元格外观?

    你可以尝试使用DataGridBoundColumn.ElementStyle 来设置样式。 并通过Trigger Property="IsMouseOver" 或者  <EventTrigger RoutedEvent="Hyperlink.Click"> 来设定对应的样式。

    Best regards

    Yong Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    单元格所属的列对象(DataGridHyperlinkColumn或DataGridColumn)怎么得到?
    2019年9月23日 12:34
  • Hi   轮回的齿轮,

    >> 单元格所属的列对象(DataGridHyperlinkColumn或DataGridColumn)怎么得到?

    参考下面的方法:

        private void OnHyperlinkClick(object sender, RoutedEventArgs e)
            {
    
                //当前对象
                Hyperlink hp = (Hyperlink)e.OriginalSource;
                //绑定行 实体对象
                User filedatasource = hp.DataContext as User;
    
                DataGridRow row = (DataGridRow)dgUsers.ItemContainerGenerator.ContainerFromItem(filedatasource);
                if (row != null)
                {
                    DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row);
    
                    DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(0);
                    if (cell != null)
                    {
                       // 根据你自己的index 获取对应的DataGridCell。
                    }
                }
    
            }
            
            public static T GetVisualChild<T>(Visual parent) where T : Visual
            {
                T child = default(T);
                int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
                for (int i = 0; i < numVisuals; i++)
                {
                    Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
                    child = v as T;
                    if (child == null)
                    {
                        child = GetVisualChild<T>(v);
                    }
                    if (child != null)
                    {
                        break;
                    }
                }
                return child;
            }
    Best regards

    Yong Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2019年9月24日 2:47
    版主