积极答复者
利用一个可生成带有超链接形式和跳转功能的TextBlock遇到了问题

问题
-
Xavier Eoro 版主及各位老师:
“http://www.codeproject.com/Tips/625509/WPF-TextBlock-with-highlighted-and-interactive-lin
”一文介绍了一种自定义的可以生成超链接形式文字和跳转功能的TextBlock控件。我想改造并利用他的功能。
一、他的自定义类代码(为说明问题,省略了有关跳转的代码,需要这可参考原文):
public class TextBlockWithLinks : TextBlock
{
static TextBlockWithLinks()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(TextBlockWithLinks), new FrameworkPropertyMetadata(typeof(TextBlockWithLinks)));
}
public TextBlockWithLinks()
{
TargetUpdated +=OnTargetUpdated;
}
private void OnTargetUpdated(object sender, DataTransferEventArgs dataTransferEventArgs)
{
FormatText(this);
}
private void FormatText(TextBlock tb)
{
var style = LinkStyle;
if (tb != null)
{
var str = tb.Text;
var hasLinks = true;
var pos = 0;
var previosPos = 0;
while (pos < str.Length-3)
{
if (str.Substring(pos, 3) == "://")
{
if (hasLinks) tb.Inlines.Clear();
var previosSpacePos = pos;
var nextSpacePos = pos;
while (str[previosSpacePos] != ' '&&previosSpacePos>0) previosSpacePos--;
while (str[nextSpacePos] != ' '&&nextSpacePos<str.Length-1) nextSpacePos++;
var linkStr = str.Substring(previosSpacePos, nextSpacePos - previosSpacePos+1);
if (previosSpacePos!=previosPos) tb.Inlines.Add(str.Substring(previosPos, previosSpacePos - previosPos));
var run = new Run(linkStr) {Style = style};
//run.MouseDown+=RunOnMouseDown;
tb.Inlines.Add(run);
previosPos = nextSpacePos;
pos = nextSpacePos;
hasLinks = false;
}
if (pos+1==str.Length-3)
tb.Inlines.Add(str.Substring(previosPos, str.Length - previosPos ));
pos++;
}
}
}
public static readonly DependencyProperty LinkStyleProperty =
DependencyProperty.Register("LinkStyle", typeof(Style), typeof(TextBlockWithLinks));
public Style LinkStyle
{
get { return (Style)GetValue(LinkStyleProperty); }
set { SetValue(LinkStyleProperty, value); }
}
}
二、界面xaml:
<Window x:Class="LinkedTextBlock.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:LinkedTextBlock"
Title="MainWindow" Height="270" Width="570">
<Window.Resources>
<Style x:Key="LinkStyle" TargetType="{x:Type Run}" >
<Style.Setters>
<Setter Property="Foreground" Value="Blue"/>
</Style.Setters>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="TextDecorations" Value="Underline"/>
<Setter Property="Cursor" Value="Hand"/>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal" Grid.Row="0">
<TextBox Text="http://www.codeproject.com/ - first link, http://google.com - second link, and the third link: http://habrahabr.ru"
Name="Input" TextWrapping="Wrap" Width="200" Margin="20" BorderBrush="Black"/>
<local:TextBlockWithLinks Width="300" TextWrapping="Wrap"
Text="{Binding Text, ElementName=Input, NotifyOnTargetUpdated=True}"
LinkStyle="{DynamicResource LinkStyle}"
x:Name="LinkedTb" Margin="20" Padding="5"/>
</StackPanel>
</Grid>
</Window>
三:程序运行结果:
我的改造想法是:在左栏提供一组含有超链接的条目,每点击一项,能够动态解析成右栏的TextBlock形式。
我改动的xaml代码如下:
<ListBox x:Name="listBox">
<sys:String>http://www.codeproject.com/ - first link</sys:String>
<sys:String>http://google.com - second link</sys:String>
<sys:String>and the third link: http://habrahabr.ru</sys:String>
</ListBox>
<local:TextBlockWithLinks Width="300" TextWrapping="Wrap"
Text="{Binding ElementName=listBox, Path=SelectedValue, NotifyOnTargetUpdated=True}"
LinkStyle="{DynamicResource LinkStyle}"
x:Name="LinkedTb" Margin="20" Padding="5"/>
我的问题是:每次运行后,只能点击生成一条对应的解析后的TextBlock形式,如下图,点击第2条结果:
再点击其他条目,就没有反映了。只能重新启动,还是只能点击生成一条。
我知道应该是绑定机制除了问题,但无法解决。请老师们费心帮我看看,先致谢意!
Ly_he
ly_he
答案
-
- 已建议为答案 Jie BaoModerator 2015年12月21日 2:42
- 已标记为答案 ly_he 2015年12月21日 8:39
- 取消答案标记 ly_he 2015年12月21日 8:40
- 已标记为答案 ly_he 2015年12月21日 8:42
全部回复
-
- 已建议为答案 Jie BaoModerator 2015年12月21日 2:42
- 已标记为答案 ly_he 2015年12月21日 8:39
- 取消答案标记 ly_he 2015年12月21日 8:40
- 已标记为答案 ly_he 2015年12月21日 8:42