Silverlight标签显示样式 [英] Silverlight label display style

查看:108
本文介绍了Silverlight标签显示样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我需要使Silverlight标签显示如下内容:S I L V E R L I G H T .
内容长度是可变的.

任何帮助将不胜感激

谢谢,
Raul

解决方案

AFAIK,没有控件可以为您执行此操作.
因此,您将需要构建自己的东西.

本文 [



和XAML:

 <   sdk:Label    高度  ="    名称  ="  labelDisplay" 宽度  自动" <   ContentControl     ="   {Binding MainContent}" > 
<  /sdk:Label  >  


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

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.


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:

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


这篇关于Silverlight标签显示样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆