我正在尝试使文本块中所有URI可点击的单词。以下是我采取的方法:
private static void onTextChanged(DependencyObject dependObj, DependencyPropertyChangedEventArgs e)
{
WrapPanel wrapPanel = ((HyperlinkTextBlock)dependObj).LayoutRoot;
wrapPanel.Children.Clear();
// TODO: use a real wordbreaker?
// adding an extra space to the end of the last word. Cry.
IList<string> words = ((string)e.NewValue).Split(' ').Select(word => word + " ").ToList();
foreach (string word in words)
{
Uri uri;
if (Uri.TryCreate(word, UriKind.Absolute, out uri))
{
// TODO the style is off - the text is too big
wrapPanel.Children.Add(new HyperlinkButton()
{
Content = word,
NavigateUri = uri,
TargetName = "_blank",
Margin = new Thickness(0),
Padding = new Thickness(0),
});
}
else
{
wrapPanel.Children.Add(new TextBlock() { Text = word, TextWrapping = TextWrapping.Wrap });
}
}
}(我完全支持一种更面向XAML/声明性的方式来做这件事,但我不确定该怎么做。)
这很好用(除了使用真正的断字符会更好),除了HyperlinkButton看起来很滑稽。它太大了,文本不会换行。它似乎也有一些偏移量,我试图通过将Margin和Padding设置为0来解决这个问题,但它没有解决这个问题。
还有其他想法吗?真的,我想要的是HyperlinkText而不是HyperlinkButton,但我不认为Windows phone7的Silverlight3能提供这样的功能。
发布于 2011-03-02 12:24:50
这是我想出的一个解决方案,通过提取超链接按钮的样式,然后对我关心的区域进行修改(如您所提到的奇怪的缩进)。这样,除了您想要更改的内容之外,行为是完全相同的。
创建超链接按钮时,还要设置样式属性,如下所示:
var button = new HyperlinkButton
{
...
Style = (Style) Resources["HyperlinkButtonWithNoIndent"]
};然后在您的页面中,将以下样式添加到资源:
<Style x:Key="HyperlinkButtonWithNoIndent" 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}" Padding="{TemplateBinding Padding}">
<TextBlock x:Name="TextElement" Text="{TemplateBinding Content}" TextDecorations="Underline" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>发布于 2010-12-30 05:05:35
我从来没有尝试过用TextBlock来做这件事,但是它可以在Image和StackPanel上工作,所以它应该也适用于你的情况。将Tap手势的处理程序添加到您的TextBlock并侦听它。然后,在事件处理程序中,可以导航到该URL。
TextBlock MyTextBlock = new TextBlock() { Text = "Tap Me!" };
GestureListener listener = GestureService.GetGestureListener( MyTextBlock );
listener.Tap += new EventHandler<GestureEventArgs>( OnMyTextBlockTapped );事件处理程序如下所示:
void OnMyTextBlockTapped( object sender, GestureEventArgs e )
{
// Navigate to URL
}您甚至可以通过在事件处理程序中启动Storyboard并在动画完成时执行导航来对轻击进行动画处理。
发布于 2010-12-30 07:15:06
你有没有试过在blend中重新电镀控件?您应该完全控制构成HyperlinkButton的所有组成部分的表示样式。
<代码>G29
您可以重用生成的样式。
https://stackoverflow.com/questions/4556927
复制相似问题