Click here to Skip to main content
15,888,031 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I need to make a silverlight label display the content like this: SILVERLIGHT.
The content length is variable.

Any help would be appreciated

Thanks,
Raul
Posted

AFAIK, there is no control that does this for you.
So you will need to build something of your own.

This article[^] might give you some ideas though.
 
Share this answer
 
I found the solution

using System;
using System.ComponentModel;
using System.Windows.Media;
using System.Windows.Controls;
using System.Windows.Documents;

namespace SilverlightApplicationTextTransform
{
    public partial class MainPage : UserControl, INotifyPropertyChanged
    {
        #region ViewModelProperty: MainContent
        private object _mainContent;
        public object MainContent
        {
            get
            {
                return _mainContent;
            }
            set
            {
                _mainContent = value;
                OnPropertyChanged("MainContent");
            }
        }
        #endregion
 
        public MainPage()
        {
            InitializeComponent();

            DataContext = this;

        }

        private char[] textToDisplay = null;
        private int length = 0;

        private void textBoxTextToDisplay_TextChanged(object sender, TextChangedEventArgs e)
        {
            textToDisplay = textBoxTextToDisplay.Text.ToCharArray();
            length = textToDisplay.Length;
            
            TextBlock tb = new TextBlock();
            tb.FontFamily = new FontFamily("Courier");
            tb.FontSize = 15;
            int fontSize = 15;

            Run[] runLabel = new Run[length];
            bool par = false; // to test if the length is odd or even
            
            if (length % 2 == 0)
            {
                par = true;
            }
            
            for (int i = 0; i < length / 2; i++)
            {
                runLabel[i] = new Run();
                runLabel[i].Text = Convert.ToString(textToDisplay[i]);
                runLabel[i].FontSize = fontSize;
                tb.Inlines.Add(runLabel[i]);

                if ((par == true) && (i == (length / 2) - 1))
                {
                }
                else
                {
                    fontSize += 3;
                }
            }
            for (int i = length / 2; i < length; i++)
            {
                runLabel[i] = new Run();
                runLabel[i].Text = Convert.ToString(textToDisplay[i]);
                runLabel[i].FontSize = fontSize;
                tb.Inlines.Add(runLabel[i]);
                fontSize -= 3;
            }
            
            MainContent = tb;
        }

        #region INotifiedProperty Block
        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;

            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
 
    }
}


And the XAML:

XML
<sdk:Label Height="Auto" Name="labelDisplay" Width="Auto">
    <ContentControl Content="{Binding MainContent}"/>
</sdk:Label>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900