【发布时间】:2014-08-22 20:52:58
【问题描述】:
我有一个对象,它当前的属性是String,并且正在后面的代码中填充。然后,此属性绑定到一个文本块,并且当前在应用程序中完全按照预期显示。
我想要做的是将字符串中的某些单词作为超链接,点击时将导航到另一个页面,但我无法实现这一点。
当我将属性更改为Paragraph 而不是String 并将文本/超链接添加为Inlines 时,我以为我已经弄清楚了,但是当我将文本块绑定到段落时,它会显示.ToString() 值,不是预期的内容。
我这样做对吗?有人可以指出我正确的方向吗?谢谢。
添加代码;
这是我的对象创建(如果我只是向AbilityEffect 添加一个字符串,那么它可以正常工作,如上所述),有两种尝试使超链接工作的方法,这两种方法都没有达到我想要的效果;
Paragraph tempparagraph = new Paragraph();
tempparagraph.Inlines.Add(new Run {Text = "Each turn the model may count one "});
tempparagraph.Inlines.Add(CreateActionHyperlink("Climb"));
tempparagraph.Inlines.Add(new Run {Text = " or one "});
tempparagraph.Inlines.Add(CreateActionHyperlink("Sprint"));
tempparagraph.Inlines.Add(new Run {Text = " action as a short action instead of a long actioin. The model may take a "});
tempparagraph.Inlines.Add(CreateActionHyperlink("Move"));
tempparagraph.Inlines.Add(new Run {Text = " action as normal."});
data.Abilities.Add(new Abilities_data
{
AbilityName = "Agile",
AbilityEffect = "Each turn the model may count one " + new HyperlinkButton
{
Content = "Climb",
NavigateUri = new Uri("/Views/Actions_Data.xaml?name=Climb", UriKind.RelativeOrAbsolute)
}
+ " or one " + new HyperlinkButton
{
Content = "Sprint",
NavigateUri = new Uri("/Views/Actions_Data.xaml?name=Sprint",UriKind.RelativeOrAbsolute)
}
+ " action as a short action instead of a long action. The model may take a " + new HyperlinkButton
{
Content = "Move",
NavigateUri = new Uri("/Views/Actions_Data.xaml?name=Move",UriKind.RelativeOrAbsolute)
}
+ " action as normal.",
AbilityEffect2 = tempparagraph
});
和 CreateAction 方法;
private Hyperlink CreateActionHyperlink(string actionname)
{
Hyperlink linky = new Hyperlink();
linky.Inlines.Add(actionname);
linky.NavigateUri = new Uri("/Views/Actions_Data.xaml?name=" + actionname,UriKind.RelativeOrAbsolute);
return linky;
}
还有我的 xaml 绑定;
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<ScrollViewer>
<TextBlock Text="{Binding AbilityEffect2}"
Style="{StaticResource PhoneTextNormalStyle}"
TextWrapping="Wrap"/>
</ScrollViewer>
</Grid>
哪个输出System.Windows.Documents.Paragraph而不是内容
【问题讨论】:
-
你能分享你的代码吗?
-
我已经添加了我的代码。干杯。
标签: c# xaml windows-phone-8