WPF / XAML - 在ListBoxItem内部拉伸超链接

时间:2018-03-21 17:43:09

标签: wpf xaml hyperlink listboxitem stretch

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

谢谢。

enter image description here

<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>

1 个答案:

答案 0 :(得分:1)

使用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>