(X) Hide this
    • Login
    • Join
      • Say No Bots Generate New Image
        By clicking 'Register' you accept the terms of use .

Silverlight LinkLabel control

(11 votes)
Emil Stoychev
>
Emil Stoychev
Joined Oct 15, 2007
Articles:   23
Comments:   98
More Articles
70 comments   /   posted on Jun 20, 2008
Categories:   Controls

This article is compatible with the latest version of Silverlight.


Introduction

In Silverlight there is a TextBlock control that is used to display simple text. However in many cases you need to display hyperlinks in the text. This article focuses on how to build such control and provides full source code for it.

* UPDATE *
Thanks to the feedback from Harlequin and his contribution now this project is published on CodePlex - http://www.codeplex.com/SilverlightLinkLabel. There you can download the source code as well as a sample project illustrating the functionality of the LinkLabel control.

Overview

In this article I take for granted that you have basic understandings how to build a custom control in Silverlight. I described the main flow in a previous post so go read it if you need to fill some gaps.

The implementation of LinkLabel control requires 3 main steps to be taken in consideration:

  1. Hyperlink recognition
  2. Text replacement
  3. Text and hyperlink arrangement

Hyperlink Recognition

First of all we need a mechanism to recognize or replace specific words in a given text - the words that will act as hyperlinks. We use two methods for this purpose:

  1. Match valid URIs - process the text and find valid URIs
  2. Match user defined hyperlink pattern - process the text and find a user defined hyperlink pattern, for example [link="URI"]text[/link]

The LinkLabel control can use either one of these methods or both in the same time.

Both methods can be implemented by using regular expressions. Lets take a look at the implementation of the second method:

 private LinkCollection GetLinkMatches()
 {
     LinkCollection uriCollection = null;
     Uri currentUri = null;
 
     Regex uriLocator = new Regex( this.LinkPattern );
     MatchCollection uriMatches = uriLocator.Matches( this.Text );
 
     // no uris found
      if ( uriMatches == null || uriMatches.Count == 0 )
      {
          return null;
      }
  
      foreach ( Match uri in uriMatches )
      {
          // not valid uri - continue with next match
          if ( !Uri.TryCreate(
              uri.Groups[ LinkPatternUriGroupName ].Value,
              UriKind.RelativeOrAbsolute, out currentUri ) )
          {
              continue;
          }
  
          if ( uriCollection == null )
          {
              uriCollection = new LinkCollection();
          }
  
          uriCollection.Add( new Link( uri.Value, currentUri )
          {
              Text = uri.Groups[ LinkPatternTextGroupName ].Value
          } );
      }
  
      return uriCollection;
  }

To simplify the code and enable the user to define what should be the hyperlink text and the hyperlink navigate URI the regular expression defines text group and link group. This way it is easy to define custom hyperlink match pattern if you don't like the default one. In general the code above matches all hyperlinks (by user defined pattern) and put them in a LinkCollection - a custom generic collection of type Link. Link is a custom class used for basic link definition with a few properties - Key, NavigateUri, TargetName and Text.

Text Replacement

After we have the links the second step is to replace them in the text.

 string linkLabelText = this.Text;
 string preUri = string.Empty;
 string[] preUriWords = null;
 string postUri = string.Empty;
 string[] postUriWords = null;
 int startIndexOfUri = 0;
 char[] delimiter = { ' ' };
 
 // no uris found
  if ( links == null || links.Count == 0 )
  {
      this.layoutRoot.Children.Add( new TextBlock()
      {
          Text = this.Text,
          Style = this.TextStyle
      } );
      return;
  }
  
  foreach ( Link link in links )
  {
      startIndexOfUri = linkLabelText.IndexOf( link.Key, StringComparison.OrdinalIgnoreCase );
      preUri = linkLabelText.Substring( 0, startIndexOfUri );
      postUri = linkLabelText.Substring( preUri.Length + link.Key.Length );
      linkLabelText = postUri;
  
      // put all the words before the current Uri
      preUriWords = preUri.Split( delimiter, StringSplitOptions.RemoveEmptyEntries );
      foreach ( string preWord in preUriWords )
      {
          this.layoutRoot.Children.Add( new TextBlock()
          {
              Text = preWord + " ",
              Style = this.TextStyle
          } );
      }
  
      // insert the Uri
      HyperlinkButton hyperlink = new HyperlinkButton()
      {
          Content = link.Text + " ",
          NavigateUri = link.NavigateUri,
          TargetName = link.TargetName,
          Style = this.LinkStyle
      };
      hyperlink.Click += new RoutedEventHandler( this.ClickLink );
      this.layoutRoot.Children.Add( hyperlink );
  }
  
  // append the text after the last uri found
  if ( !string.IsNullOrEmpty( linkLabelText ) )
  {
      postUriWords = postUri.Split( delimiter, StringSplitOptions.RemoveEmptyEntries );
      foreach ( string postWord in postUriWords )
      {
          this.layoutRoot.Children.Add( new TextBlock()
          {
              Text = postWord + " ",
              Style = this.TextStyle
          } );
      }
  }

The above code is the core of the LinkLabel control. The method takes a LinkCollection as an input parameter and iterates over it. Here is the process explained:

  1. For each link in the collection the text before its position is assigned to a local variable preUri.
  2. preUri is split by spaces and each word is set to the Text property of a new TextBlock control.
  3. The TextBlocks are added as child elements in the layoutRoot.
  4. A HyperlinkButton control is created and also added as a child element in the layoutRoot

This process continues until all hyperlinks replaced and added to the layoutRoot. Finally if there is any text left after the last hyperlink it is also added in the layoutRoot.

Text and Hyperlink Arrangement

Now it gets prettier. We have made the replacements in the text with the desired links and add all TextBlocks and HyperlinkButtons to the layoutRoot. What type of control actually is the layoutRoot? The answer is WrapPanel. By using a WrapPanel we can easily arrange the text and the hyperlinks so they don't exceed the container's area. When a control is added in the panel it is placed next to the other controls on the line. If the line size is not enough to contain the control it is automatically placed on the next line.

WrapPanel control is not included in the current version of the framework. Anyway there are a number of open source Silverlight WrapPanel controls you can use. A great article in CodeProject explains how to implement a WrapPanel by yourself and also provides a complete source code. The implementation is good, however it needs some customization (more specificly the MeasureOverride method) to make it work as a real WrapPanel. I won't get into this here but you can read this article to learn more on the topic.

Summary

It is so native to put hyperlinks into a text that I haven't even thought this can't be achieved with Silverlight out of the box. The implementation of the LinkLabel control is not complex at all and maybe this is the main reason it is not included in the framework. I didn't dive too much in the details but there is source code so you can take a look and understand it by yourself.

References

Creating a Silverlight Custom Control - The Basics

Silverlight WrapPanel

MeasureOverride method implementation for the WrapPanel


Subscribe

Comments

  • -_-

    RE: Silverlight LinkLabel control


    posted by Fallon on Jul 04, 2008 00:42

    How hard would it be to account for the following.

    1. Open Link in the current window
    2. Execute C# method or event
    3. Open Link in tab

    Just wondering.

    Thanks, Fallon...

  • emil

    RE: Silverlight LinkLabel control


    posted by emil on Jul 08, 2008 05:17

    Hi Fallon,

    This is pretty easy actually. If you have already download the source code you can see there is an internal class named Link. There is a property TargetName which for now is just hard coded to "_self". If you make a good regular expression that can match [link="...." target=".."]...[/link] you can use the target value and set it to TargetName. This way you will be able to set in which window the link to be opened. 

    Same is for executing a method or event. You can define an Event in the Link class and then attach to it when adding the Links.

    I can actually implement these features in the next version. If you have interest in that I would like to hear. Thank you.

  • -_-

    RE: Silverlight LinkLabel control


    posted by James Roe on Sep 02, 2008 16:27

    There are a couple of bugs with this current implementation. First and foremost the regex breaks on url's with slashes after the initial slash.

    ie:
    http://videosift.com - works
    http://videosift.com/ - doesn't work

    you can fix this by replacing the default uri pattern on line 15 of LinkLabel.cs with the following code

    public const string DefaultUriPattern = "(^|[ \t\r\n])((ftp|http|https|gopher|mailto|news|nntp|telnet|wais|file|prospero|aim|webcal):(([A-Za-z0-9$_.+!*(),;/?:@&~=-])|%[A-Fa-f0-9]{2}){2,}(#([a-zA-Z0-9][a-zA-Z0-9$_.+!*(),;/?:@&~=%-]*))?([A-Za-z0-9$_+!*();/?:~-]))";

    also if you want to add an underline to your links the way it is currently adding spacing to the end of links will result in the underline extending past the edge of the text. This can be fixed by changing line 255 from:

    Content = link.Text + " ",

    to:

    Content = link.Text,

    and by adding

                    this.layoutRoot.Children.Add(new TextBlock()
                    {
                        Text = " ",
                        Style = this.TextStyle
                    });
     

    following this.layoutRoot.Children.Add( hyperlink ); on line 261

    one final bug that I haven't resolved yet is managing punctuation that occurs directly after a link, for the time being in my apps I am just including the punctuation in the link tag, but I know some designers who would find that abhorable. Hope this helps someone.

  • -_-

    RE: Silverlight LinkLabel control


    posted by James Roe on Sep 03, 2008 01:15

    One final note, word wrapping does not occur if there is no link in the text.

  • -_-

    RE: Silverlight LinkLabel control


    posted by Harlequin on Sep 15, 2008 09:58

    When does the LinkLabels OnApplyTemplate() get hit? I put a breakpoint in my OnApplyTemplate() and it never gets in there, thus the WrapPanel never gets inserted.

  • emil

    RE: Silverlight LinkLabel control


    posted by emil on Sep 16, 2008 05:16

     Harlequin,

    Are you sure you enabled debugging for Silverlight?

    I set a breakpoint in the OnApplyTemplate() method and stepping into the code the sequnce is:

    this.RootVisual = new Page();
    - Page constructor
    -- InitializeComponent();
    -- ...
    and after that OnApplyTemplate()

    Do you experience any specific problem?

  • -_-

    RE: Silverlight LinkLabel control


    posted by Harlequin on Sep 16, 2008 10:19

    Specific problem...I didn't add the generic.xaml to my Core project (where I keep the C# files)...thus there was no template to run. =)

    Just one more thing not working now, I should have it fixed soon then I should be good to go.

  • -_-

    RE: Silverlight LinkLabel control


    posted by Harlequin on Sep 16, 2008 11:05

    As for James and the TextWrapping I see in the AddLinks function there is a function when there are no links, I just added Wrap in there by hand, but you might wanna make it I guess so the user can choose to wrap or nowrap as a property.

    // no uris found
    if (links == null || links.Count == 0)
    {
      this.layoutRoot.Children.Add(new TextBlock()
      {
        Text = this.Text,
        Style = this.TextStyle,
        TextWrapping = TextWrapping.Wrap    <-- I added this, and wrapping worked when no links.
      });
      return;
    }

  • -_-

    RE: Silverlight LinkLabel control


    posted by Harlequin on Sep 16, 2008 11:21

    Working nice so far. Only thing I'd like to iron out is to be able to use the text in the Xaml itself. Doesn't seem to take it for some reason.

    <local:LinkLabel x:Name="MyLinkLabel">
      <local:LinkLabel.Text>
        Here's some text in the xaml.    <-- Doesn't work
      </local:LinkLabel.Text>
    </local:LinkLabel>

  • iiordanov

    RE: Silverlight LinkLabel control


    posted by iiordanov on Sep 18, 2008 05:21

    Hi Harlequin,

    Thank you very much for your comments!
    We are going to update the source code of the link label control with the great improvements you suggested!

  • -_-

    RE: Silverlight LinkLabel control


    posted by Rodrigo on Sep 18, 2008 07:23

    Hi,

    i was wondering if i can use your code to finish my web page  >> www.centrodeinovacao.org.br <<

    your solution solve all my problems.

    Can i use it?

  • iiordanov

    RE: Silverlight LinkLabel control


    posted by iiordanov on Sep 18, 2008 07:43

    Hi Rodrigo,

    Yes, you are absolutely free to use the LinkLabel control in any projects you would like!
    It is completely free and it is made for the community.

  • -_-

    RE: Silverlight LinkLabel control


    posted by Rodrigo on Sep 18, 2008 11:54

    Thanks!!!

    You guys saved my life =]

  • -_-

    RE: Silverlight LinkLabel control


    posted by Harlequin on Sep 19, 2008 07:48

    http://silverlight.net/forums/t/27546.aspx

    I started a thread on the Silverlight forums to see if we can get content right into the Xaml itself. Text="test" works as an attribute for some reason, but can't get it to work like this:
    <local:LinkLabel x:Name="MyLinkLabel">
      <local:LinkLabel.Text>
        test
      </local:LinkLabel.Text>
    </local:LinkLabel>

    Even better of course would be able to do this, but damned if I can't get that working :)
    <local:LinkLabel x:Name="MyLinkLabel">
        test
    </local:LinkLabel>

  • -_-

    RE: Silverlight LinkLabel control


    posted by Harlequin on Sep 19, 2008 14:54

    Also, has anyone tried grabbing the WrapPanel.cs from the link above? I use it, and even with the override tweaks, my Scrollviewer doesn't scroll. Almost like the WrapPanel isn't telling the Scrollviewer how much height it needs.

  • emil

    RE: Silverlight LinkLabel control


    posted by emil on Sep 20, 2008 01:21

    Hi Harlequin,

    About this: 
    <local:LinkLabel x:Name="MyLinkLabel">
        test
    </local:LinkLabel>

    As answered in the Silverlight.net Forums a ContentProperty attribute is required for this to work.

    About
    <local:LinkLabel x:Name="MyLinkLabel">
      <local:LinkLabel.Text>
        test
      </local:LinkLabel.Text>
    </local:LinkLabel>

    I really don't know why it doesn't work. I've tried it myself and couldn't get it working also. Just as you I thought setting the attribute like <control Attribute=""> and setting it like <Control.Attribute>...</....> is the same. Well, seems it isn't or we are both missing something.

    It is easier to set the text property directly between opening and closing the LinkLabel tag. It is a good idea and I'll implement that in the control so everyone can use it that way. On Monday I'll publish a new version with this feature.

    The WrapPanel used in this control is a free control posted on CodeProject - check out the article here. There is also an open thread where other users using this control are experiencing the same issue - http://www.codeproject.com/KB/silverlight/WrapPanelSilverlight.aspx?msg=2483575#xx2483575xx.

     

     

     

  • -_-

    RE: Silverlight LinkLabel control


    posted by Harlequin on Sep 22, 2008 09:05

    Thanks for the response.

    Is there a way the to be able to use single quotes in the special [link ""][/link] element then, since we need to use the attribute, we can't be using double quotes. I don't care much about the scrollviewer not working right now, since the next projects for Microsoft I'll be working on won't need scrolling since all blocks of text will have a predetermined height/width, so I should be okay.

    Not sure why ContentProperty or the other stuff doesn't work, I tried everything myself, but using Silverlight you start to get a 6th sense that something you're trying to fix is actually a Beta 2 bug. Which I think this is.

  • -_-

    RE: Silverlight LinkLabel control


    posted by Harlequin on Sep 22, 2008 10:20

    And for the wrapping problem, where there are no links, getting more intimate with the code I found that if there are no links it makes 1 big TextBlock in the AddLinks() function, and when there are links, it pretty much chops up the entire thing to make TextBlocks and HyperlinkButtons. So, in the code below I made it so if it's text only, it chops it all up word by word and adds a TextBlock.. Helps the WrapPanel calculate things better. Might be a bit inefficient on bigger pages, would have to try it out. Of course a better way would be to find out it's allowed width, and set the one big TextBlock to that width, instead of chopping it up like I did.

    My code in the AddLinks for no Uris.

    // no uris found
    if (links == null || links.Count == 0)
    {
      string[] allwords = linkLabelText.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
      foreach (string word in allwords)
      {
        this.layoutRoot.Children.Add(new TextBlock()
        {
          Text = word + " ",
          Style = this.TextStyle
        });
      }
    }

  • -_-

    RE: Silverlight LinkLabel control


    posted by Harlequin on Sep 22, 2008 14:43

    Here's how you add functionality for a target="" attribute on the [link] element. All changes are in LinkLabel.cs. I'll assume you'll know where all the objects go, since they're named like the Uri and Text ones.

    New LinkPattern:
    public const string DefaultLinkPattern = "\\[link=\"(?<link>(.|\n)*?)\"\\s*(target=\"(?<target>(.|\n)*?)\")?](?<text>(.|\n)*?)\\[\\/link\\]";

    New constant string:
    public const string DefaultLinkPatternTargetGroupName = "target";

    New private string:
    private string linkPatternTargetGroupName;

    New GroupName, this can go under the "public string LinkPatternTextGroupName" item:
    /// <summary>
    /// Gets or sets the GroupName of the target in the RegularExpression.Match
    /// </summary>
    public string LinkPatternTargetGroupName
    {
      get
      {
        if (this.LinkPattern.Equals(DefaultLinkPattern, StringComparison.OrdinalIgnoreCase))
        {
          return DefaultLinkPatternTargetGroupName;
        }

        return this.linkPatternTargetGroupName;
      }
      set
      {
        this.linkPatternTargetGroupName = value;
      }
    }

    In the GetLinkMatches() function inside the foreach(), here is a new uriCollection.Add:
    uriCollection.Add(new Link(uri.Value, currentUri)
    {
      Text = uri.Groups[LinkPatternTextGroupName].Value,
      TargetName = uri.Groups[LinkPatternTargetGroupName].Value
    });

     

    That's it, now this should work for ya:
    [link="http://www.cnn.com" target="_blank"]CNN[/link]

    Only thing I think that is missing is if there is no target="", then I'm not quite sure what the TargetName is set to in the uriCollection.Add, might need a default or something.

  • -_-

    RE: Silverlight LinkLabel control


    posted by Harlequin on Sep 22, 2008 14:53

    Just changed the uriCollection.Add this this, so the default is "_self". Make the default whatever you'd want of course:
    uriCollection.Add(new Link(uri.Value, currentUri)
    {
      Text = uri.Groups[LinkPatternTextGroupName].Value,
      TargetName = (uri.Groups[LinkPatternTargetGroupName].Value == String.Empty ? "_self" : uri.Groups[LinkPatternTargetGroupName].Value)
    });

  • emil

    RE: Silverlight LinkLabel control


    posted by emil on Sep 23, 2008 06:58

    Wow, Harlequin, thank you for the feedback!

    I'm glad you are so active here!

    Because of you, I considered to take this control into CodePlex. Now, I'm adding what I promised (the ContentProperty attribute) and I'll publish the latest source code on CodePlex.

    I like the changes you made about the target attribute. It was in my schedule also, but you did it already and that's great.

    My proposal for you is to contribute your changes in CodePlex. I'll publish the project today or tomorrow early in the morning (GMT +2) so then you can make your changes if you like the idea. Let me first publish the project and I'll share the link.

    What do you think? 

  • -_-

    RE: Silverlight LinkLabel control


    posted by Harlequin on Sep 23, 2008 07:09

    Good to hear about the ContentProperty, couldn't get that bloody <local:LinkLabel.Text> thing to work :)

    ++ on the CodePlex. Will be a lot easier for this thing to grow on CodePlex than in a blog entry with a flat comment listing.

  • -_-

    RE: Silverlight LinkLabel control


    posted by Harlequin on Sep 23, 2008 07:12

    Oh yeah, I'll be using this thing on an internal Silverlight thingy for Microsoft today(starting 3 week Silverlight project), so it's another Silverlight project being dogfooded by Microsoft :)

  • emil

    RE: Silverlight LinkLabel control


    posted by emil on Sep 24, 2008 02:33

    Harlequin, I've bumped my head with the ContentProperty attribute yesterday, but it turns out that in the current version of Silverlight this attribute doesn't work. I've talked with other people in the Silverlight community and they reported the same. I checked how this is done in the TextBlock control and saw the there is an internal attribute ContentProperty which is a bit different from the public one. So we have to wait and see if this will be fixed in the next release.

    BUT, there are some good news also ;) I've just published the control on CodePlex - http://www.codeplex.com/SilverlightLinkLabel with your changes for the target attribute. I didn't applied your change for the "AddLinks for no Uris" because I don't see a good point in it, but, of course, we can discuss that also.

    If you are willing to add new features or just some changes I can add you as a contributor in the CodePlex project. For this purpose I need your username.

    Thanks again ;)

  • -_-

    RE: Silverlight LinkLabel control


    posted by Harlequin on Sep 24, 2008 07:52

    Sure I'll sign up there....once they fix the registration :)

    Yeah, the "AddLinks for no Uris" was kind of a workaround for the wrappanel textwrapping problem since the measuring stuff in the wrappanel project isn't working right. I noticed with a long block of text with no links it would canculate the height as being 42 pixels and the width as 5500 pixels, so the wrapping was never taking place. But when I ripped apart and sent in a bunch of TextBlocks it could do it properly.

    *Geez*, new it was a bug when it wasn't working at all any way I tried it. They need a Silverlight bug list. Wasting too much precious time getting things to work when it's all "fixed in RTM". *Arrg*

    When it's in CodePlex we'll need to change the RegEx so we can use single quotes in that [link] tag as well as double quotes. Otherwise you'll never be able to make text with links in the Xaml itself, only in the C# which really won't do.......until the RTM comes out of course :)

  • emil

    RE: Silverlight LinkLabel control


    posted by emil on Sep 24, 2008 09:34

    Instead of  " use &quot;

    This is the way you can write qutoes(") in the XAML.

  • -_-

    RE: Silverlight LinkLabel control


    posted by ChaosKiller on Oct 10, 2008 06:51

    Is it possible to have an Silverlight hyperlink change the page currently displayed in an ASP.NET (.aspx) content place holder? Or can this be done using iFrames? Basically I have a Silverlight menu, and the links displayed here need to change just the content in the content place holder, instead or loading the whole web page. Hope that makes sense. Thanks

  • -_-

    RE: Silverlight LinkLabel control


    posted by Harlequin on Oct 10, 2008 09:18

    ChaosKiller: Yeah. When you're making the click event for links in here you can put in calls to JavaScript perhaps, then your JavaScript on the page can do AJAX stuff, if that's the kind of thing you're looking for.

    Emil: Thanks. will need to check out RC0 I guess and see if we can put Content into a control like we want to. Nice to be able to do that instead of being stuck in the attribute.

  • -_-

    RE: Silverlight LinkLabel control


    posted by Rodrigo on Oct 21, 2008 10:49

    Hey guys.

    i'm trying to migrate my website from Beta 2 to RTW.

    I tried to use the link label, and didn't work. There is a problem in this part of the code on LinkLabel.cs

    private void ClickLink( object sender, RoutedEventArgs e )
            {
                LinkClickEventHandler click = this.LinkClick;
                if ( click != null )
                {
                    LinkClickEventArgs args = new LinkClickEventArgs( ( sender as HyperlinkButton ).NavigateUri );
                    args.Source = this;
                   
                    click( this, args );
                }
            }

    "args. Source" doesn't exist in the new version of Silverlight.

    I'm trying to fix, but it's a little complicated...

    If someone can help it would be great.

    thanks

  • -_-

    RE: Silverlight LinkLabel control


    posted by Rodrigo on Oct 21, 2008 10:50

    sorry for the duplicated post... :P

  • emil

    RE: Silverlight LinkLabel control


    posted by emil on Oct 22, 2008 10:06

    Rodrigo,

    the control has been migrated to Silverlight 2 RTW. Just download the latest binaries from http://www.codeplex.com/SilverlightLinkLabel/SourceControl/ListDownloadableCommits.aspx and you are good to go ;)

  • -_-

    RE: Silverlight LinkLabel control


    posted by Rodrigo on Oct 22, 2008 10:40

     thanks guys =]

  • -_-

    RE: Silverlight LinkLabel control


    posted by Matt on Nov 18, 2008 22:04

    Isn't there already a Hyperlink control???

    Matt
    VinSolutions Automotive Software
     

  • emil

    RE: Silverlight LinkLabel control


    posted by emil on Nov 20, 2008 01:52

    Matt,

    there is a hyperlink control, but it only displays hyperlinks. In the LinkLabel control you can have both text and hyperlinks. Get your comment for example - it contains both text and a link to your company. Without a LinkLabel control you can't display your comment in a Silverlight application. That's the main purpose of this control.

    Hope this helps.

  • -_-

    RE: Silverlight LinkLabel control


    posted by Garf on Dec 04, 2008 02:32

    How can I make the Hyperlink wrap?

  • -_-

    RE: Silverlight LinkLabel control


    posted by Scott Barnes on Jan 26, 2009 01:28

    I just noticed that if you have a URL like this:

    http://www.silverlight.net/myshow/ as your hyperlink. the /myshow/ causes LinkLabel to throw an index zero error under the for loop for Link in links.

    ---

    Scott Barnes
    Rich Platforms Product Manager
    Microsoft.

  • -_-

    RE: Silverlight LinkLabel control


    posted by Brian Norman on Mar 14, 2009 12:03
    codeplex is down so thought Id discuss and issue here. If you have a single line of text, with no links in, that is longer than the control the linklabel is a child of the text will not wrap.

    From looking at the source this is becuase if a single (with no links) string of text is displayed you just create a single textblock and add this to the wrap panel. Unfortunately this means it does not wrap for long lines of text.

    I have done a quick bodge in my code that splits the text into words and adds each as a textblock in the same way you do when there are links in the text but it needs some refactoring to not be a bodge.

    For uses like the I have, where I do not know if the text has links in or not until its displayed this is a critical issue.

    Thanks for the great control and I hope this fix will help others wondering why it doesnt work

    Brian @ Earthware.co.uk
  • emil

    RE: Silverlight LinkLabel control


    posted by emil on Mar 18, 2009 06:09
    Hi Brian,

    Just replied to your issue in CodePlex.
  • -_-

    RE: Silverlight LinkLabel control


    posted by rollo on Mar 31, 2009 17:48
    I have been unable to get this to work... Has anyone else run into problems? I have tried added the .dll as a resource to blend 2. When I use the control it throws an error.

    While trying to build the LinkLabel in VS2008 I get errors about WrapPanel?!?! Saying it is a namespace but is used like a type...

    I am new to silverlight and my coding skills are rusty so need a little extra hand holding here.
  • -_-

    RE: Silverlight LinkLabel control


    posted by Rajan on Apr 19, 2009 02:38

    I'm having one more problem when use Expression Blend 3 preview.

    Seleced Link Label control and added to my page. After adding the control getting error "the element could not be displayed because of a problem with completIT.windows.controls.linklabel"

    Please help me to solve this problem.

     

  • -_-

    RE: Silverlight LinkLabel control


    posted by Angelo Santos on May 20, 2009 10:05
    Error 1 'CompletIT.Windows.Controls.LinkLabelElements.LinkCollection' is inaccessible due to its protection level  anyone can help !? :( thank you
  • emil

    RE: Silverlight LinkLabel control


    posted by emil on May 20, 2009 10:38
    Hi Angelo,

    What are you trying to do? How do you get this error?
  • -_-

    RE: Silverlight LinkLabel control


    posted by Angelo Santos on May 20, 2009 11:58
    Putting the LinkCollection method in my Project after adding all the necessary references.Is there something else that i must do after putting the imports and putting the references to both dlls  :(  ?

    and also the "LinkLabel project " gives an error at compile time

    Error 2 'CompletIT.Windows.LinkClickEventArgs' does not contain a definition for 'Source' and no extension method 'Source' accepting a first argument of type 'CompletIT.Windows.LinkClickEventArgs' could be found (are you missing a using directive or an assembly reference?) D:\Documents and Settings\angelo.santos\Desktop\LinkLabel\LinkLabel\LinkLabel.cs 231 22 LinkLabel

    thank you for your response :)

     

     

  • emil

    RE: Silverlight LinkLabel control


    posted by emil on May 21, 2009 01:48
    Did you download the latest source code from CodePlex? http://silverlightlinklabel.codeplex.com/SourceControl/changeset/view/11245

    I just did and it compiles without any problems. Be sure to use Silverlight 2. It is not tested on SL3.
    Also if you are using the latest release of the Silverlight Toolkit then you must change the Microsoft.Windows.Controls reference to System.Windows.Controls.Toolkit in the LinkLabel project.
  • -_-

    RE: Silverlight LinkLabel control


    posted by Angelo Santos on May 21, 2009 06:36
    no way :( ... javascript Error... and on aspx nothing happens and sometimes out of memory errors ...
    what i did was change to put this project working (11245)

    private WrapPanel layoutRoot;
    to

    private

     

     WrapPanel.WrapPanel layoutRoot;

    Deleted microsoft.windows.controls

    and put the System.Web.Silverlight.dll file in bin folder of the LinkLabelWeb Project and it compiled that way

    but when i run the test projects javascript error on LinkLabelTest and out of memory erros on the LinkLabelTestTestPage.aspx project  

    here are my SL versions 

    Visual Studio 2008 SP1
    Silverlight plug in - 2.0.40115.0
    Silverlight 2 SDK - 2.0.31005.0
    Silverlight 2 SDK Toolkit March 2009- 2.0.30318.1329
    Silverlight Tools for visual studio 2008 SP1 - ENU -  9.0.30729.146

    thank you for your patience :)

     

     



     

     

     




     

     

     

     

  • -_-

    RE: Silverlight LinkLabel control


    posted by Angelo Santos on May 21, 2009 08:53

    can anyone please put the version that is on this page please on line (http://www.silverlightshow.net/showcase/LinkLabelTest.xap)... thats the only one that work with me :(
    could it be something from the debuger ? im trying through the debuger :(

    thanks

     

     

  • emil

    RE: Silverlight LinkLabel control


    posted by emil on May 21, 2009 10:21
  • -_-

    RE: Silverlight LinkLabel control


    posted by Angelo Santos on May 22, 2009 06:29
    Can anyone tell me how can i change the Styles ? (links and text) ...thank you
  • -_-

    RE: Silverlight LinkLabel control


    posted by Angelo Santos on Jun 03, 2009 12:29
    Please how can i change font style in Links ...? thank you again
  • emil

    RE: Silverlight LinkLabel control


    posted by emil on Jun 04, 2009 01:13
    Hi Angelo,

    you can use LinkStyle property to define the font style like this:
    <Style x:Key="MyLinkStyle" 
            TargetType="HyperlinkButton"
            <Setter Property="FontFamily" 
                    Value="Arial" /> 
            <Setter Property="FontSize" 
                    Value="14" /> 
    </Style> 

    Hope that helps.
  • -_-

    RE: Silverlight LinkLabel control


    posted by WarNov on Jun 28, 2009 19:03
    Hi. I hace Slverlight 2 Relases in my browser. But when I Compiled the sample project, the resulting page suggests me to download silverlight. Why is that? Is this for SL 3?
  • -_-

    RE: Silverlight LinkLabel control


    posted by WarNov on Jun 28, 2009 21:39
    Does somebody have a complete working example? I couldn't get codeplex sources work fine. I don't know why they don't just publish a single rar with everythig working :S. The "last" sources throwme out of memory exception
  • -_-

    RE: Silverlight LinkLabel control


    posted by warnov on Jun 29, 2009 02:01

    Well. Now Im not agry anymore, so Im going to describe well my situation:

    1. I've first downloaded the sample+dll. The compiled xap, worked perfectly.
    2. Downloaded sources, but when compiling LinkLabel, had 2 errors:
      1. Wrap not found
      2. Windows.Controls not found
        1. Then read the annotations and:
    3. Installed last version of Codeplex Silverlight Toolkit
    4. Referencied System.Windows.Controls.Toolkit
    5. Deleted the un-working reference
    6. Rebuild, run and: An unhandled exception of type 'System.OutOfMemoryException' occurred in Unknown ModuleCreated a fresh new project
    7. Added the just Buil LinkLabel.dll
    8. Build, run and: InvalidOperationException
    9. Replaced my built LinkLabel.dll with the one available in codeplex:
    10. Build and: Could not load file or assembly 'Microsoft.Windows.Controls, Version=2.0.21027.1502, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.

     Please somebody help me. I've spent a complete day trying to make this work. I would like to have the eaxct project source for the xap download tab in codeplex, because when running that web project it worked very fine. Any other advice will be very welcome. Thanks a lot.

    warnov@gmail.com. http://warnov.com/

  • -_-

    RE: Silverlight LinkLabel control


    posted by WarNov on Aug 05, 2009 06:20
    Is there any Silverlight Updated Version?
  • emil

    RE: Silverlight LinkLabel control


    posted by emil on Aug 05, 2009 09:47
    WarNov, I'll take a look at it today. I guess you are talking about Silverlight 3, right?
    I'll see whether there are some compatibility issues and fix them.
  • -_-

    RE: Silverlight LinkLabel control


    posted by Ben Hurtisson on Dec 29, 2009 13:34
    Greatest instructions for usage of SilverLight.

    Web design services


  • -_-

    RE: Silverlight LinkLabel control


    posted by Zsolt Hever on Jan 11, 2010 01:03
    SilverLight 3 update

    The WrapPanel makes the problem.
    To solve the problem:

    LinkLabel class library:
    Remove reference for Microsoft.Windows.Controls
    and add reference to System.Windows.Controls.Toolkit instead.

    LinkLabel.cs
    Remove using Microsoft.Windows.Controls;

    Themes/generic.xaml
    Add
    xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit"
    instead of
    xmlns:controls="clr-namespace:Microsoft.Windows.Controls;assembly=Microsoft.Windows.Controls"

    Compile and use the new LinkLabel.dll in Silverlight 3
  • -_-

    RE: Silverlight LinkLabel control


    posted by Zain Shaikh on Feb 12, 2010 20:49

    can any one please tell me why does it not create the hyperlink of middle link when i pass this string to LinkLabel Control?

    note: the middle link's text and action is changed, if i keep all the links with same text and action, it works, but when i change the text or action of middle link, it does not convert it to hyperlink.

    [link="google"]google[/link] [link="googlez"]googlez[/link] [link="google"]google[/link]

    any help please???

  • biper4x4

    RE: Silverlight LinkLabel control


    posted by biper4x4 on Sep 25, 2010 20:14
    Best post ever about URL in text on Silverlight. Probably I will use it on my page Clever Notes
  • -_-

    RE: Silverlight LinkLabel control


    posted by Robert on Nov 15, 2010 15:45
    How about Silverlight 4?
  • -_-

    RE: Silverlight LinkLabel control


    posted by Jake on Feb 22, 2011 22:10

    Thanks for the sample. I was looking for a complete example for DependencyProperty and this was really nice! Modified it to run without a browser and upgraded the code for SL4. Thanks!!

  • lnikolov

    RE: Silverlight LinkLabel control


    posted by lnikolov on Feb 25, 2011 18:12
    The article has been updated to the latest version of Silverlight and Visual Studio.
  • -_-

    RE: Silverlight LinkLabel control


    posted by CrisRowlands on May 16, 2011 11:35

    Hiya, Im just wondering if something like this could be altered or created to work with silverlight for WindowsPhone?

    Im currently working on updating the 1800PocketPC.com app, making my own html parser which converts articles into a more 'mobile friendly' view. Having something like this that worked in wp7 apps would be a godsend.

  • emil

    RE: Silverlight LinkLabel control


    posted by emil on May 16, 2011 11:43

    Hi Cris,

    Yes, it is possible to such this control on WP7 too. I think you will only have to build the project for WP7 and it should work. If not - the source code is available on CodePlex so you can make the required changes. If you want to contribute to the project - let me know.

    Emil

  • -_-

    RE: Silverlight LinkLabel control


    posted by CrisRowlands on May 16, 2011 12:15

    Thanks for the surprisingly quick response :)
    That sounds great, but sadly I don't actually know how to rebuild the project for wp7.

    I'm just about managing to get by with the little coding knowledge I have & binging everything I don't know :P

  • -_-

    RE: Silverlight LinkLabel control


    posted by Jacky on May 29, 2011 19:57

    Grat article! I also sometimes write about silver light and windows phone,  if you want just follow my site:)

    Greetings.

  • AndrzejAndrzejewski

    Re: Silverlight LinkLabel control


    posted by AndrzejAndrzejewski on Jul 07, 2011 12:18

    Hi Jacky! Nice site, I must use translator but you have very useful informations!

    I also want t invite you tomy blog about Windows 7 phone

  • leolivio

    Re: Silverlight LinkLabel control


    posted by leolivio on Jan 18, 2012 01:37
    I have loaded the LinkLabel.dll I add in the references but it is not compatible to the version system.Windows.Controls 2.0.5.0 use Silverlight 4  ??
     
    help 
     
    thanks 
     
    Livio
  • emil

    Re: Silverlight LinkLabel control


    posted by emil on Jan 18, 2012 09:59

    The control is compatible with Silverlight 4. I haven't tested it under Silverlight 5, but from what I see it is working correctly.

  • leolivio

    Re: Silverlight LinkLabel control


    posted by leolivio on Jan 18, 2012 15:00

    Notice 1 Assembly 'Microsoft.Windows.Controls, Version=2.0.21027.1502, Culture=neutral, PublicKeyToken=31bf3856ad364e35' could not be loaded and will be ignored. 
    Impossible to load the file or the assembly 'Microsoft.Windows.Controls, Version=2.0.21027.1502, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of the relative dependences. Impossible to find the specified file. SilverlightIMVCC

    this error signals when I load the DLL ??

Add Comment

Login to comment:
  *      *