代码之家  ›  专栏  ›  技术社区  ›  Johannes Setiabudi

如何为具有数据绑定内容/文本的超链接按钮设置包装?

  •  6
  • Johannes Setiabudi  · 技术社区  · 14 年前

    我将一个集合(RSS feed)绑定到一个列表框中,例如:

    <ListBox Margin="0,0,-12,0" ItemsSource="{Binding Items}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Margin="0,0,0,17" Width="432">
                    <HyperlinkButton Content={Binding Title} NavigateUri="{Binding Link}" />
                    <TextBlock Text="{Binding Description}" />
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    

    这很好-数据显示正确等等。但是现在当我把它改为使用文字环绕时,标题就不再显示了。

    这是有问题的代码。

    <ListBox Margin="0,0,-12,0" ItemsSource="{Binding Items}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Margin="0,0,0,17" Width="432">
                    <HyperlinkButton NavigateUri="{Binding Link}">
                        <TextBlock Text="{Binding Title}" TextWrapping="Wrap" />
                    </HyperlinkButton>
                    <TextBlock Text="{Binding Description}" TextWrapping="Wrap" />
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    

    我不认为是“textfwrapping”属性导致了这个问题,因为我尝试不使用它,但它仍然不起作用。所以我的问题是,你是如何让这样的东西发挥作用的?我只想显示一个带有包装绑定文本的超链接。这看起来是一件相当简单的事情,但却很难做到。帮助?

    2 回复  |  直到 12 年前
        1
  •  7
  •   Steve    13 年前

    我也遇到了同样的问题,我不明白为什么在我遇到之前它都不起作用。 this blog 邮寄 Mister Goodcat . 在他的文章中,他说由于对Silverlight中的WP7进行了一些优化,在普通Silverlight中工作的基本功能在WP7 Silverlight中无法正常工作。他建议改为修改样式,而不是使用。

    编辑默认模板的最简单方法仍然是使用ExpressionBlend复制默认模板并从中进行工作。当您这样做时,您会看到模板实际上只有一个文本块来显示内容。这就是为什么使用另一个UI元素作为内容对于WP7上的超链接按钮不起作用。如果只希望进行文本换行,则只需更改该文本块上的TextWrapping属性即可。

    <Style x:Key="HyperlinkButtonWrappingStyle"
            TargetType="HyperlinkButton">
        <Setter Property="Foreground"
                Value="{StaticResource PhoneForegroundBrush}" />
        <Setter Property="Background"
                Value="Transparent" />
        <Setter Property="FontSize"
                Value="{StaticResource PhoneFontSizeMedium}" />
        <Setter Property="Padding"
                Value="0" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="HyperlinkButton">
                    <Border Background="Transparent">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal" />
                                <VisualState x:Name="MouseOver" />
                                <VisualState x:Name="Pressed">
                                    <Storyboard>
                                        <DoubleAnimation Duration="0"
                                                            To="0.5"
                                                            Storyboard.TargetProperty="Opacity"
                                                            Storyboard.TargetName="TextElement" />
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Disabled">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground"
                                                                        Storyboard.TargetName="TextElement">
                                            <DiscreteObjectKeyFrame KeyTime="0"
                                                                    Value="{StaticResource PhoneDisabledBrush}" />
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Border Background="{TemplateBinding Background}"
                                Margin="{StaticResource PhoneHorizontalMargin}"
                                Padding="{TemplateBinding Padding}">
                            <TextBlock x:Name="TextElement"
                                        HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                        Text="{TemplateBinding Content}"
                                        TextDecorations="Underline"
                                        VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                        TextWrapping="Wrap" />
                        </Border>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    

    我建议阅读他的博客文章以获取更多信息和帮助。

        2
  •  1
  •   Richard    13 年前

    下面是问题的解决方案: http://www.pitorque.de/MisterGoodcat/post/WP7-snippet-analyzing-the-hyperlink-button-style.aspx#continue

    上面显示的代码在SL4中可以正常工作,但在WindowsPhone7中不能正常工作;主要是出于性能原因。使用Blend编辑超链接按钮样式,在该样式中,您将发现超链接按钮已使用文本块显示内容,并添加textWrapping=“Wrap”属性。