简体   繁体   中英

Create a hyperlink(programatically) to a file from an OpenFileDialog and putting it in a TextBlock

I'm working on a Instant Messaging UI assignment and one of the requirements is the ability to ''send'' files, this app isn't connected to the net so it basically just has to show up in the output TextBlock. And the idea is that it should open on click, my first idea was to combine hyperlinks and OpenFileDialog but hyperlinks are unknown territory for me.

My idea was the try an OpenFileDialog, and get the file name from it and somehow turn it into a hyperlink and add it to the output TextBlock, but I'm not even sure how.

Heres the open file dialog code(unfinished and bad since i hit a brick wall):

        private void FileSelect_Click(object sender, RoutedEventArgs e)
    {
        string fileName;

        OpenFileDialog OFD = new OpenFileDialog();

        OFD.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        OFD.Title = "Select a file to send";
        fileName = OFD.FileName;

        if(OFD.ShowDialog() == true)
        {
            Hyperlink hype = new Hyperlink();
           //..........
        }
    }

And the hypothetical file is suppose to be sent in the output TextBlock:

<TextBlock Margin="5,0,0,0" Name="tbDestination" 
                                            ScrollViewer.HorizontalScrollBarVisibility="Auto"
                                            ScrollViewer.VerticalScrollBarVisibility="Auto"
                                            ScrollViewer.CanContentScroll="True">
                </TextBlock>

Any sort of tip on how handle this kind of problem is welcome. I'm still kind of new to this so I don't know the ropes.

You have to add a hyperlink element to your xaml window :

  <TextBlock>           
    <Hyperlink NavigateUri="http://myurl">
        <TextBlock Name="mylink"> </TextBlock>
    </Hyperlink>
  </TextBlock>

then you set the text property of the "mylink" textblock

private void FileSelect_Click(object sender, RoutedEventArgs e)
{
    string fileName;

    OpenFileDialog OFD = new OpenFileDialog();

    OFD.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
    OFD.Title = "Select a file to send";
    fileName = OFD.FileName;

    if(OFD.ShowDialog() == true)
    {
        this.mylink.text = fileName ;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM