WPF / XAML - 拉伸 ListBoxItem 内的超链接

WPF / XAML - Stretch hyperlink inside of ListBoxItem

我想拉伸黄色文本块以填满整个栏。换句话说,我想用黄色覆盖蓝色,使超链接可以从左到右点击。请指教

谢谢。

<UserControl x:Class="LSS_doc.Views.ResultTabView"
         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" 
         mc:Ignorable="d" 
         d:DesignHeight="450" d:DesignWidth="800">
<Grid>
    <ListBox Name="FileList" ItemsSource="{Binding Result}" Tag="{Binding AcceptedKeywordsArray}">
        <ListBox.ItemContainerStyle>
            <Style TargetType="{x:Type ListBoxItem}">
                <Setter Property="Focusable" Value="False"/>
                <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
            </Style>
        </ListBox.ItemContainerStyle>
        <ListBox.ItemTemplate>
            <DataTemplate DataType="string">
                <TextBlock Background="Blue" HorizontalAlignment="Stretch">
                    <Hyperlink CommandParameter="{Binding ElementName=txtBlock}" Command="{Binding DataContext.DisplayFile, ElementName=AppMainWindow}">
                        <Hyperlink.Resources>
                            <Style TargetType="{x:Type Hyperlink}">
                                <Style.Triggers>
                                    <Trigger Property="IsMouseOver" Value="True">
                                        <Setter Property="Foreground" Value="blue" />
                                    </Trigger>
                                </Style.Triggers>
                                <Setter Property="Foreground" Value="Black" />
                                <Setter Property="TextBlock.TextDecorations" Value="{x:Null}" />
                            </Style>
                        </Hyperlink.Resources>
                        <TextBlock Name="txtBlock" Text="{Binding}" Tag="{Binding Tag, ElementName=FileList}" Margin="0,0,0,0" HorizontalAlignment="Stretch" Background="Yellow"/>
                    </Hyperlink>
                </TextBlock>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>

使用 Button 并定义自定义 ControlTemplate 使其看起来像超链接:

<ListBox.ItemTemplate>
    <DataTemplate>
        <Button CommandParameter="{Binding ElementName=txtBlock}" Command="{Binding DataContext.DisplayFile, ElementName=AppMainWindow}" Cursor="Hand">
            <Button.Template>
                <ControlTemplate TargetType="Button">
                    <TextBlock x:Name="tb" Text="{Binding}" />
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter TargetName="tb" Property="Foreground" Value="blue" />
                            <Setter TargetName="tb" Property="TextDecorations" Value="Underline" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Button.Template>
        </Button>
    </DataTemplate>
</ListBox.ItemTemplate>