微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

silverlight – 如何使用数据绑定内容/文本设置HyperlinkBut​​ton的包装?

我将一个集合(RSS Feed)绑定到一个列表框中,如下所示:

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

这很好用 – 数据显示正确等等.但是现在当我将其更改为使用文本换行时,标题不再显示.

这是有问题的代码.

<ListBox Margin="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”属性,因为我试过没有它,但它仍然无法正常工作.所以我的问题是你如何得到这样的东西?我只想显示包装绑定文本的超链接.看起来这是一件相当简单的事情 – 但还是那么难.救命?

解决方法

我遇到了完全相同的问题,我无法理解为什么它在 Mister Goodcat之前运行 this blog帖子之前没有工作.在他的帖子中他说,因为对Silverlight中的WP7进行了一些优化,所以基本功能可用于正常Silverlight无法正常运行WP7 Silverlight.而不是使用他建议修改样式而不是.

The easiest way to edit the default templates still is to use Expression Blend to make a copy of it and work from there. When you do that you’ll see that the template really only has a text block to show the content. That is why using another UI element as content doesn’t work for the hyperlink button on WP7. If you only want to make the text wrap,it’s sufficient to change the textwrapping property on that text block.

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

我建议阅读他的博客文章了解更多信息&救命.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐