简体   繁体   English

从OpenFileDialog创建(以编程方式)到文件的超链接并将其放在TextBlock中

[英]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. 我正在处理即时消息UI分配,其中一项要求是“发送”文件的能力,该应用程序未连接到网络,因此它基本上只需要显示在输出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. 我的想法是应该在单击时打开它,我的第一个想法是将超链接和OpenFileDialog结合起来,但是超链接对我来说是未知领域。

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. 我的想法是尝试使用OpenFileDialog,并从中获取文件名,然后以某种方式将其转换为超链接,然后将其添加到输出TextBlock中,但是我什至不知道如何。

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中发送:

<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 : 您必须在xaml窗口中添加一个超链接元素:

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

then you set the text property of the "mylink" textblock 然后设置“ mylink”文本块的text属性

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 ;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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