【问题标题】:WPF / XAML - Stretch hyperlink inside of ListBoxItemWPF / XAML - 在 ListBoxItem 内拉伸超链接
【发布时间】:2018-08-30 23:32:07
【问题描述】:

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

谢谢。

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

【问题讨论】:

    标签: wpf xaml hyperlink listboxitem stretch


    【解决方案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>
    

    【讨论】:

      猜你喜欢
      • 2018-08-02
      • 1970-01-01
      • 1970-01-01
      • 2018-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-05
      相关资源
      最近更新 更多