Welcome to MSDN Blogs Sign in | Join | Help
'paragraphing' in Silverlight 1.1

I recently had to whip up a small application to demonstrate the programmatic interface for silverlight 1.1.  Using a tag cloud as a suitable project, I quickly became aware of the difficulty of X-Y only positioning for that type of scenario.

 I came up with a solution using the 'ActualHeight' and 'ActualWidth' properties:
            string[] tags = new string[] { "asp.net", "sqlServer", "bookies", "babaganoush", "frankie", "johnnie" };

            double currentX = 0;

            double currentY = 0;

            double highestHeight = 0;

 

            foreach (string tag in tags)

            {

                TextBlock tagblock = new TextBlock();

                tagblock.Text = tag;

                tagblock.FontFamily = "Verdana";

                tagblock.FontSize = 10;

                tagblock.Foreground = new SolidColorBrush(Colors.Black);

                tagblock.MouseEnter += new MouseEventHandler(Tag_MouseEnter);

                tagblock.MouseLeave += new EventHandler(Tag_MouseLeave);

                highestHeight = (tagblock.ActualHeight > highestHeight) ? tagblock.ActualHeight : highestHeight;

 

                if ((currentX != 0) && (currentX + tagblock.ActualWidth > this.TagCloudCanvas.Width))

                {

                    // reset width & go to the next line

                    currentX = 0;

                    currentY = currentY + highestHeight + 5;

                    highestHeight = 0;

                }

 

                tagblock.SetValue(Canvas.LeftProperty, currentX);

                tagblock.SetValue(Canvas.TopProperty, currentY);

 

                this.TagCloudCanvas.Children.Add(tagblock);

 

                currentX = currentX + tagblock.ActualWidth + 5;

            }

HTH!

Posted: Monday, November 26, 2007 12:51 PM by mr.m
New Comments to this post are disabled
Page view tracker