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>

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

相关文章

如何在Silverlight4(XAML)中绑定IsEnabled属性?我试过简单的...
我正在编写我的第一个vb.net应用程序(但我也会在这里标记c#,...
ProcessFile()是在UIThread上运行还是在单独的线程上运行.如...
我从同行那里听说,对sharepoint的了解对职业生涯有益.我们不...
我正在尝试保存一个类我的类对象的集合.我收到一个错误说明:...
我需要根据Silverlight中的某些配置值设置给定控件的Style.我...