C# 删除WPF中动态超链接的下划线
我创建WPF应用程序。在某种形式下,用户将richtextbox的所选文本更改为hyperlink。我搜索了一个多小时,寻找解决方案。但是不能。C# 删除WPF中动态超链接的下划线,c#,wpf,hyperlink,richtextbox,text-decorations,C#,Wpf,Hyperlink,Richtextbox,Text Decorations,我创建WPF应用程序。在某种形式下,用户将richtextbox的所选文本更改为hyperlink。我搜索了一个多小时,寻找解决方案。但是不能。 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid x:Name="Root"> <TextBlock x:Nam
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid x:Name="Root">
<TextBlock x:Name="TextBl"></TextBlock>
</Grid>
</Window>
我的动态超链接创建如下:
var textRange = RichTextBox.Selection;
textRange.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Blue);
var hyperlink = new Hyperlink(textRange.Start, textRange.End)
{
IsEnabled = true,
Foreground = Brushes.Blue
};
hyperlink.NavigateUri = new Uri("http://search.msn.com/" + firstOrDefault.WordId);
var main = new WordMain();
hyperlink.Click += new RoutedEventHandler(main.hyperLink_Click);
RichTextBox.IsDocumentEnabled = true;
RichTextBox.IsReadOnly = false;
如何删除动态超链接的下划线。我想使用textdecoration,但不能通过代码来实现。我刚刚尝试了这个方法,效果很好
Xaml
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid x:Name="Root">
<TextBlock x:Name="TextBl"></TextBlock>
</Grid>
</Window>
从
C#
代码中删除颜色格式,并将其放入App.xaml
文件中:
<Application.Resources>
...
<TextDecorationCollection x:Key="_textDeco_hyperlink">
<!--<TextDecoration Location="Underline" />-->
</TextDecorationCollection>
<Style TargetType="{x:Type Hyperlink}">
<Setter Property="Foreground" Value="Black"/>
<Setter Property="Background" Value="Khaki" />
<Setter Property="ForceCursor" Value="True" />
<Setter Property="Cursor" Value="Hand" />
<Setter Property="TextDecorations" Value="{DynamicResource _textDeco_hyperlink}" />
<Setter Property="IsEnabled" Value="False" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Foreground" Value="Khaki" />
<Setter Property="Background" Value="Black" />
</Trigger>
</Style.Triggers>
</Style>
...
<Application.Resources>
对于从搜索结果中找到简单答案且不想保留任何装饰(例如使用图像时)的任何人:
...
很抱歉,有件事我不明白:你还需要保留其他文字装饰吗?或者你能把它们全部删除吗?@Elvin Mammadov:你的应用程序中的所有超链接都应该有相同的格式吗?是的。我将这些超链接添加到数据库中,当我向richtextbox添加文本时,@lyz的代码起作用。但从数据库检索后,超链接下划线又出现了,但当我从数据库加载它时,下划线又出现了。您的文本范围是什么?您将超链接添加到哪个元素?如果您可以显示更多代码,请将超链接添加到哪个元素?我在您更新的代码中没有看到它。这里没有其他元素。
// Store your FlowDocument as a list of strings
List<string> blocksAsStrings = FlowDoc_store(_docSelected._Rtb.Document);
...
// Load your FlowDocument into your RichTextBox
rtb.Document = FlowDoc_load(blocksAsStrings);
/// <summary>
/// Stores a FlowDocument as a list of strings, each string represents a Block.
/// </summary>
public static List<string> FlowDoc_store(FlowDocument flowDoc)
{
List<string> blocksAsStrings = new List<string>(flowDoc.Blocks.Count);
foreach (Block block in flowDoc.Blocks)
{
blocksAsStrings.Add(XamlWriter.Save(block));
}
return blocksAsStrings;
}
/// <summary>
/// Loads a FlowDocument from a list of strings, each string represents a Block.
/// </summary>
public static FlowDocument FlowDoc_load(List<string> blocksAsStrings)
{
FlowDocument flowDoc = new FlowDocument();
foreach (string blockAsString in blocksAsStrings)
{
using (StringReader stringReader = new StringReader(blockAsString))
{
using (XmlReader xmlReader = XmlReader.Create(stringReader))
{
Block block = (Block)XamlReader.Load(xmlReader);
flowDoc.Blocks.Add(block);
}
}
}
return flowDoc;
}