-1

I'm printing out a url from a data set within my xaml file. The issue is, is that I'm required to only create a hyperlink if the string is a valid URL.

I know how to do this in most languages, but not in WPF. I'm looking for something that works as;

if(isurl)
    display hyperline gridrow
else 
    display string gridrow

My xaml at present is;

<u:GridViewColumnEx x:Uid="column_4" Width="225" >
    <GridViewColumn.CellTemplate>
        <DataTemplate>
            <TextBlock>
                <Hyperlink>
                    <TextBlock Text="{Binding Data.path}"/>
                </Hyperlink>
            </TextBlock>
        </DataTemplate>
    </GridViewColumn.CellTemplate>
</u:GridViewColumnEx>

But this displays everything in the column as a hyperlink (obviously). I've read about triggers but am not sure if this is what I'm after. I've read quite a bit about triggers, but am unsure on how to alter the xaml based on the return value.

H.B.
  • 166,899
  • 29
  • 327
  • 400
Dannys
  • 335
  • 1
  • 5
  • 12
  • 4
    "_I know how to do this in most languages, but not in WPF_" So do you know how to do it in C#? Because WPF is not the language.. – Khalil Khalaf Aug 19 '16 at 15:45
  • Sorry, I understand the c# side (mostly) but not the XAML, should have made that clear; I.E i'll be able to create a method in c# to i.e return true if it detects a url or false if it's not a url. But my issue would be applying that value to the XAML to either display the url or norma text. – Dannys Aug 19 '16 at 15:47
  • 3
    One: http://stackoverflow.com/questions/7578857/how-to-check-whether-a-string-is-a-valid-http-url – Khalil Khalaf Aug 19 '16 at 15:48
  • 3
    Two: http://stackoverflow.com/questions/924679/c-sharp-how-can-i-check-if-a-url-exists-is-valid – Khalil Khalaf Aug 19 '16 at 15:48
  • 2
    Three (marked as duplicate but also has an answer): http://stackoverflow.com/questions/16079685/how-to-check-whether-the-url-is-valid-or-not – Khalil Khalaf Aug 19 '16 at 15:49
  • Please do some basic research before asking any questions. We would like to help. But it is already there and you just need to look for it. – Khalil Khalaf Aug 19 '16 at 15:50
  • Regarding your comment edit: To display as hyper link or not that is a separate question. Solve the C# side and then post a new question something like "I have true or false, how do I display hyper link or normal". – Khalil Khalaf Aug 19 '16 at 15:51
  • Both of them show how to detect a URL, but neither show how to apply this to XAML to display based on the return value of the method, which is my issue. – Dannys Aug 19 '16 at 15:52
  • @firstStep , you have mentioned links which solve one part of the problem i.e. check if the string is a valid URL, what about the other part i.e. if the string is not valid it should be a plain text otherwise a hyperlink. i doubt you have read the whole question. go easy on the guy, everybody has the right to ask questions. – Sameed Aug 19 '16 at 15:53
  • @sameed what is not being easy with my comments? I am **actually** helping him.. I read the whole question. Did you read all my comments? :D And what is your comment helping him with? – Khalil Khalaf Aug 19 '16 at 15:54
  • @FirstStep "I.E i'll be able to create a method in c# to i.e return true if it detects a url or false if it's not a url.". My comment made it clear that I knew how to do all 3 of the comments. The edit was because I didn't know you couldn't press enter to tab down in a comment. – Dannys Aug 19 '16 at 15:56
  • as a matter of fact i did. I doubt that you read the full question at first. all you did is pasted links, which he could have found himself. – Sameed Aug 19 '16 at 15:57
  • .. You know, I think I helped more than enough :) Good luck. – Khalil Khalaf Aug 19 '16 at 15:58
  • I understand the question probably isn't worded the best, but basically what I was trying to say is, based on a value set in a method in c#, how do I display a certain xaml line, I have two versions of what I want to display; one that displays a string and one that displays a url. – Dannys Aug 19 '16 at 15:59

2 Answers2

1

One way to solve this problem is to make a boolean property which indicates the validity of the URL. Then in XAML you can have two controls in the datatemplateof your GridViewColumn.CellTemplate one a hyperlink and other a TextBlock. you have to change the visibility of the controls based on the boolean property by using triggers:

                <DataTemplate>
                <Grid>
                    <TextBlock Visibility="Collapsed" Name="Hyperlink">
            <Hyperlink NavigateUri="{Binding Data.path}">
            </Hyperlink>
                    </TextBlock>
            <TextBlock Visibility="Collapsed" Name="simpleText"
                       Text="{Binding Data.path}"></TextBlock>
                </Grid>
                <DataTemplate.Triggers>
                    <DataTrigger Binding="{Binding Data.IsValidURL}" //your boolean property
                                 Value="True">
            <Setter TargetName="Hyperlink"
                                Property="Vibility"
                                Value="Visible" />
                    </DataTrigger>
            <DataTrigger Binding="{Binding Data.IsValidURL}" //your boolean property
                                 Value="False">
            <Setter TargetName="simpleText"
                    Property="Vibility"
                    Value="Visible" />
            </DataTrigger>
            <DataTemplate.Triggers>
            </DataTemplate>
Sameed
  • 655
  • 1
  • 5
  • 18
1

To check if string is a valid url:

bool CheckUrl(string urlString)
{
  if(Uri.TryCreate(urlString, UriKind.Absolute, out uriResult))
  { 
    return (uriResult.Scheme == Uri.UriSchemeHttp);
  }
  return false;
}

To make the text a hyperlink based on true or false remove the hyperlink element from the Xaml. You will create a hyperlink programmatically based on the url.

if(CheckUrl(urlString))
{
   TextBlock.Inlines.Add(new Hyperlink (new Run("Click me")){NavigateUri = new Uri("urlString",UriKind.Absolute) });
}
else 
{
   TextBlock.Text = urlString;
}
Hassaan Akbar
  • 195
  • 3
  • 12