简体   繁体   English

未为DataGridHyperlinkColumn触发“ Hyperlink.Click”事件

[英]“Hyperlink.Click” event not being fired for DataGridHyperlinkColumn

I have a WPF form with a DataGrid containing multiple DataGridHyperlinkColumn , with a Hyperlink.click handler set up. 我有一个WPF表单,其中的DataGrid包含多个DataGridHyperlinkColumn ,并设置了Hyperlink.click处理程序。

GamesGrid.xaml: GamesGrid.xaml:

<UserControl
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:SteamWishlist"
             x:Name="gamesGridControl" x:Class="MyProgram.GamesGrid"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <DataGrid x:Name="dataGrid" AutoGenerateColumns="False" CanUserSortColumns="False" SelectionUnit="Cell" SelectionMode="Single" AreRowDetailsFrozen="True" CanUserResizeRows="False" >
            <DataGrid.Columns>
                <DataGridHyperlinkColumn ClipboardContentBinding="{x:Null}" Binding="{Binding Url}" ContentBinding="{Binding Name}" Header="Name">
                    <DataGridHyperlinkColumn.ElementStyle>
                        <Style>
                            <EventSetter Event="Hyperlink.Click" Handler="DG_Hyperlink_Click"/>
                        </Style>
                    </DataGridHyperlinkColumn.ElementStyle>
                </DataGridHyperlinkColumn>
                <DataGridHyperlinkColumn ClipboardContentBinding="{x:Null}" Binding="{Binding InstallLink}" Header="Install">
                    <DataGridHyperlinkColumn.ElementStyle>
                        <Style>
                            <EventSetter Event="Hyperlink.Click" Handler="DG_Hyperlink_Click"/>
                        </Style>
                    </DataGridHyperlinkColumn.ElementStyle>
                </DataGridHyperlinkColumn>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</UserControl>

GamesGrid.xaml.cs: GamesGrid.xaml.cs:

public partial class GamesGrid : UserControl
{
    ...
    private void DG_Hyperlink_Click(object sender, RoutedEventArgs e)
    {
        Hyperlink link = (Hyperlink)e.OriginalSource;
        Process.Start(link.NavigateUri.AbsoluteUri);
    }
}

A few weeks ago this exact code worked just fine, but today the event is suddenly not being fired - if I set a breakpoint in DH_Hyperlink_Click , it's never reached. 几周前,此确切的代码运行良好,但今天该事件突然不被触发-如果我在DH_Hyperlink_Click设置一个断点,则永远无法实现。

I'm not sure where to even start debugging this issue. 我不确定从哪里开始调试此问题。 Has anyone else encountered this before? 有人遇到过吗?

Of course, the problem turned out to be yet another completely random WPF bug, sigh. 当然,这个问题竟然是另一个完全随机的WPF错误。

Apparently if you set DataGrid.ItemSource after an await inside a TextBox.LostKeyboardFocus callback, it breaks the DataGridHyperlinkColumn.Hyperlink.Click event. 显然,如果在TextBox.LostKeyboardFocus回调内部await之后设置DataGrid.ItemSource ,则会中断DataGridHyperlinkColumn.Hyperlink.Click事件。 Why? 为什么? I have no idea. 我不知道。

I tried everything I could think of to work around the issue, but nothing worked. 我尝试了所有可以解决的问题,但没有任何效果。 In the end I had to stop using await inside the callback and handle asynchronous events manually. 最后,我不得不停止在回调中使用await并手动处理异步事件。 Sigh. 叹。

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

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