Damian
Damian

Reputation: 175

WPF / XAML - Stretch hyperlink inside of ListBoxItem

I would like to stretch the yellow textblock to fill the entire column. In other words I want to cover the blue color with the yellow one to make the hyperlinks clickable from the left to the right. Please advise.

Thank you.

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>

Upvotes: 2

Views: 223

Answers (1)

mm8
mm8

Reputation: 169340

Use a Button and define a custom ControlTemplate to make it look like a hyperlink:

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

Upvotes: 1

Related Questions