Silverlight text around an image

后端 未结 2 1816
忘掉有多难
忘掉有多难 2021-01-24 09:23

Im am trying to wrap text around an image as one would use the html float property. Is there a way to acomplish this in silverlight 3?

Thanks

2条回答
  •  不知归路
    2021-01-24 10:03

    I tackled this issue a while back. There is not really a good way that I know of. This will work though it's just painful.

    In order to simplify the explanation why don't we assume that the image is in the upper right corner of the page and the text needs to be to the left and below the image.

    Start by placing the TextBlock and the Image side-by-side.

    Calculate the bottom most point of the TextBlock and the bottom most point of the image. (Use their top margins and actual heights.

    While the TextBlock is the larger you move a word at a time into a newly created TextBlock below the image. This creates the illusion of wrapping text.

        leftText.Text = textToWrap;
        bottomText.Text = string.Empty;
        Stack wordsToMove = new Stack();
        double imageBottomPoint = image1.ActualHeight + image1.Margin.Top;
        while ((leftText.ActualHeight + leftText.Margin.Top) > (imageBottomPoint + 14))
        {
            int lastSpace = leftText.Text.LastIndexOf(' ');
            string textToMove = leftText.Text.Substring(lastSpace).Trim();
            BlockedText.Text = leftText.Text.Remove(lastSpace);
            wordsToMove.Push(textToMove + ' ');
        }
        StringBuilder sb = new StringBuilder(bottomText.Text);
        while (wordsToMove.Count > 0)
        {
            sb.Append(wordsToMove.Pop());
        }
    
        bottomText.Text = sb.ToString();
    

提交回复
热议问题