如何在WPF中将文本框文本设置为超链接

kid*_*ida 2 xaml wpf-controls wpf-4.0

我想将文本框文本设为超链接.假设如果将www.google.com类型作为tetxbox文本,当我点击文本时,它会显示在浏览器中打开链接.

我无法实现这一点......你能不能向我提出任何想法......我尝试了两种方式.

WAY1:

  <TextBox Grid.Row="4" Name="txtWebPage" VerticalAlignment="Top" TextDecorations="UnderLine" TextChanged="txtWebPage_TextChanged" Foreground="Blue">
                                </TextBox>
Run Code Online (Sandbox Code Playgroud)

way2:

 <TextBlock Name="tbWebpage" Grid.Row="4" Background="White" VerticalAlignment="Top" Height="20" >
                                    <Hyperlink></Hyperlink>
                                </TextBlock>
Run Code Online (Sandbox Code Playgroud)

way3:

   <RichTextBox Grid.Row="4" Name="rtxtWeb" BorderBrush="Gray" BorderThickness="1" VerticalAlignment="Top" Height="20" IsDocumentEnabled="True" Foreground="Blue" LostFocus="rtxtWeb_LostFocus">
                                    <FlowDocument>
                                        <Paragraph>
                                            <Hyperlink NavigateUri=""/>
                                        </Paragraph>
                                    </FlowDocument>
                                </RichTextBox>
Run Code Online (Sandbox Code Playgroud)

我无法得到如何将RichTextBox文本绑定到Hyperlink uri!richtextbox没有点击事件......任何建议请...

jos*_*112 7

首先,我不确定你为什么要这样做...如果文本成为可点击的超链接,它是一个有效的URI,你将如何继续编辑它?

Hyperlink控件不会对您执行任何特殊操作,也不能在TextBox中托管.相反,使用常规TextBox,每次更新时检查文本是否有效的URI,并应用样式使文本看起来像一个可点击的链接.

<TextBox TextChanged="TextBox_TextChanged" MouseDoubleClick="TextBox_MouseDoubleClick">
    <TextBox.Style>
        <Style TargetType="TextBox">
            <Style.Triggers>
                <DataTrigger Binding="{Binding HasValidURI}" Value="True">
                    <Setter Property="TextDecorations" Value="Underline"/>
                    <Setter Property="Foreground" Value="#FF2A6DCD"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBox.Style>
</TextBox>
Run Code Online (Sandbox Code Playgroud)

每次更改文本时,TextBox_TextChanged都会调用.这将检查文本是否是使用的有效URI Uri.TryCreate().如果HasValidURI是,则将属性设置为true.一个DataTriggerTextBox's样式发现这一点,使文本下划线和蓝色.

使超链接立即可点击将导致您无法定位光标,所以我注意双击.当收到一个时,再次将文本转换为URI并启动Process该URI.

public partial class MainWindow : Window, INotifyPropertyChanged
{
    private bool _hasValidURI;

    public bool HasValidURI
    {
        get { return _hasValidURI; }
        set { _hasValidURI = value; OnPropertyChanged( "HasValidURI" ); }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged( string name )
    {
        var handler = PropertyChanged;
        if( handler != null ) handler( this, new PropertyChangedEventArgs( name ) );
    }

    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;
    }

    private void TextBox_TextChanged( object sender, TextChangedEventArgs e )
    {
        Uri uri;
        HasValidURI = Uri.TryCreate( (sender as TextBox).Text, UriKind.Absolute, out uri );
    }

    private void TextBox_MouseDoubleClick( object sender, MouseButtonEventArgs e )
    {
        Uri uri;
        if( Uri.TryCreate( (sender as TextBox).Text, UriKind.Absolute, out uri ) )
        {
            Process.Start( new ProcessStartInfo( uri.AbsoluteUri ) );
        }
    }
}
Run Code Online (Sandbox Code Playgroud)