【问题标题】:How to set wrapping for HyperlinkButton with databound Content/Text?如何使用数据绑定的内容/文本为 HyperlinkBut​​ton 设置换行?
【发布时间】:2011-05-10 21:55:03
【问题描述】:

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

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

我不认为是“TextWrapping”属性导致了问题,因为我尝试不使用它,但它仍然无法正常工作。所以我的问题是你如何让这样的事情发挥作用?我只想显示一个带有包装的绑定文本的超链接。这看起来是一件相当简单的事情 - 但又是如此困难。帮忙?

【问题讨论】:

  • 我有完全相同的代码用于包装为 DataGrid Header 的一部分,它工作正常。
  • 我不知道为什么你的网格可以工作而我的不行。我只是在VS 2010中使用全景项目的启动项目模板并将TextBlock基本上更改为HyperlinkBut​​ton。

标签: silverlight data-binding silverlight-4.0 hyperlink


【解决方案1】:

我遇到了完全相同的问题,直到遇到Mister Goodcatthis blog 帖子,我才明白为什么它不起作用。在他的帖子中,他说由于 Silverlight 中为 WP7 所做的一些优化,在普通 Silverlight 中工作的基本功能在 WP7 Silverlight 中无法正常工作。他建议修改样式而不是使用。

编辑默认模板的最简单方法仍然是使用 Expression Blend 复制它并从那里开始工作。当您这样做时,您会看到模板实际上只有一个文本块来显示内容。这就是为什么使用另一个 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】:

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

    上面显示的代码在 SL4 中可以正常工作,但在 Windows Phone 7 中则不行;主要是出于性能原因。使用 Blend 编辑 HyperlinkBut​​ton 样式,您会发现 HyperlinkBut​​ton 已经使用 TextBlock 进行内容显示并添加 TextWrapping="Wrap" 属性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多