【问题标题】:trying to add and display hyperlink to string property of an object尝试添加和显示对象的字符串属性的超链接
【发布时间】: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


【解决方案1】:

Paragraph 元素不适用于TextBlock。您需要使用RichTextBox。您需要将其IsReadOnly 属性设置为true,否则Hyperlink 将不会处于活动状态。此外,您需要将此Paragraph 添加到RichTextBoxBlocks 属性中。 Blocks 不是依赖属性,因此您不能对它使用数据绑定。

你可以这样做:

<RichTextBox IsReadOnly="True" TextWrapping="Wrap" x:Name="rtb"/>

Paragraph tempparagraph = new Paragraph();
tempparagraph.Inlines.Add(new Run { Text = "Each turn the model may count one " });
tempparagraph.Inlines.Add(CreateActionHyperlink("Climb"));
.........

rtb.Blocks.Add(tempparagraph);

编辑:

你可以看看这个例子:Bind text with links to RichTextBox。它使用RichTextBox 的派生版本并添加了一个可数据绑定的Text 属性。

编辑2:

再想一想,我认为您可以执行以下操作来对现有代码进行最小的更改:

(1) 像以前一样使用RichTextBlock

<RichTextBox IsReadOnly="True" TextWrapping="Wrap" x:Name="rtb"/>

(2) 在页面的 cs 文件中为视图模型的 PropertyChanged 事件添加处理程序。

(this.DataContext as Abilities_data).PropertyChanged += OnAbilitiesdataPropertyChanged;

(3) 如果AbilityEffect2 属性发生变化,请将其添加到RichTextBlock

void OnAbilitiesdataPropertyChanged(object sender, PropertyChangedEventArgs e)
{
    if (e.PropertyName == "AbilityEffect2")
    {
        rtb.Blocks.Clear();
        rtb.Blocks.Add((sender as Abilities_data).AbilityEffect2);
    }
}

我可能弄错了你的视图模型的结构,但你明白了。

希望这会有所帮助。

【讨论】:

  • 感谢您的帮助,今晚晚些时候我会尝试一下。这是实现带有超链接的文本块的最佳/唯一方法吗?从我对 WPF 的(诚然有限的)理解来看,需要使用不支持绑定的只读输入控件或创建自己的版本和鞋拔绑定似乎有点违反直觉。我原以为 MS 有一个更简单的方法来做到这一点......
  • 是的,就是这样做的。但是,我找到了一种更简单的方法。请参阅我更新的答案的“编辑 2”部分。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-06
  • 2020-03-30
  • 2017-05-24
相关资源
最近更新 更多