WPF文字排列方式解析zz


 

WPF文字的處理是一個比較基礎的技能。在使用WPF開發工具時,對於各種文字的處理時經常會遇到的情況。希望大家可以通過實踐經驗的積累,牢固掌握這一方面知識。

AD:WOT2014:用戶標簽系統與用戶數據化運營培訓專場

WPF文字在處理的過程中可以實現許多種方式來滿足我們開發人員在實際編程中的需求。在這里將會為大家呈現一種WPF文字作為標題時的豎排方式。

有時Expande 控件的標題文字需要豎排,例如 Expande的FlowDirection屬性為"RightToLeft",即左右方向的收。

WPF文字的處理相關代碼示例:

  1. < Grid x:Name="gridTemplate">
  2. < Grid.Resources>
  3. < !--模板數據的Expender標題豎排-->
  4. < DataTemplate x:Key=
    "ExpanderHeaderTextV">
  5. < TextBlock Text="{Binding}"
  6. Width="30"
  7. Foreground="Green"
  8. FontSize="20"
  9. FontWeight="Normal"
  10. TextWrapping="Wrap">
  11. < TextBlock.RenderTransform>
  12. < TransformGroup>
  13. < MatrixTransform/>
  14. < /TransformGroup>
  15. < /TextBlock.RenderTransform>
  16. < Run Text="模"/>
  17. < LineBreak/>
  18. < Run Text="版"/>
  19. < LineBreak/>
  20. < Run Text="內"/>
  21. < LineBreak/>
  22. < Run Text="容"/>
  23. < LineBreak/>
  24. < /TextBlock>
  25. < /DataTemplate>
  26. < /Grid.Resources>
  27. < Expander HorizontalAlignment=
    "Stretch" Header="" HeaderTemplate=
    "{StaticResource ExpanderHeaderTextV}
    " ExpandDirection="Left"
    FlowDirection="RightToLeft"
    VerticalAlignment="Stretch"
    AllowDrop="False">
  28. < TabControl IsSynchronizedWith
    CurrentItem="True" Margin=
    "0,0,0,0" FontSize="14">
  29. < TabItem Header="模板數據"
    x:Name="tabTemplate">
  30. < Grid/>
  31. < /TabItem>
  32. < /TabControl>
  33. < /Expander>
  34. < /Grid>

WPF文字的基本處理方法就為大家介紹到這里。

 

--------------------------------------------------------------------------------------------------------------------------------------

 

Silverlight提供的TextBlock 用於基本的文本顯示. 通常狀態下,TextBlock文本都是橫排狀態.但是,有的時候,我們需要文本豎排以滿足我們的需求. 下面介紹一下兩種方法:

方法一: 使用TextWrapping = Wrap

TextBlock的TextWrapping屬性用於獲取或設置TextBlock對文本的換行方式,它有兩個枚舉值:NoWrap和Wrap.Wrap表示允許TextBlock當文本的長度超過TextBlock的ActualWith時,文本自動換行.這個屬性給我們一點啟示:我們可以設置TextWrapping = Wrap,並設定TextBlock的With來實現我們的效果.

例如:

<TextBlock TextAlignment="Center" TextWrapping="Wrap" Text="好友列表" Width="12" Foreground="Red"/>

效果:

但是,這個方法有一個缺點,就是你需要設置好TextBlock.的with屬性和文本字體大小的比例. 例如,我們將依舊設置成width = 12, FontSize =20,即:

<TextBlock TextAlignment="Center" TextWrapping="Wrap" Text="好友列表" Width="12" Foreground="Red" FontSize="20" />

得到的效果:

很明顯文字有被遮擋,因此,如果再給字體添加一個粗體的屬性,那遮擋效果更明顯.於是,尋求另一中方法.

方法二:自定義一個繼承與Control的類,命名為VerticalTextBlock.

該類公布一個TextProperty屬性,並自定義控件模板,在模板中添加一個TextBlock,TextBlock的Text內容由一系列帶有換行符的字符組成.該類也重寫模板應用方法.代碼如下:

復制代碼

public class VerticalTextBlock:Control
    {
public VerticalTextBlock()
        {
            IsTabStop = false;
            var templateXaml =
@"<ControlTemplate " +
#if SILVERLIGHT
"xmlns='http://schemas.microsoft.com/client/2007' " +
#else
"xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' " +
#endif
"xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>" +
"<Grid Background=\"{TemplateBinding Background}\">" +
"<TextBlock x:Name=\"TextBlock\" TextAlignment=\"Center\"/>" +
"</Grid>" +
"</ControlTemplate>";
#if SILVERLIGHT
            Template = (ControlTemplate)XamlReader.Load(templateXaml);
#else
using(var stream = new MemoryStream(Encoding.UTF8.GetBytes(templateXaml)))
            {
                Template = (ControlTemplate)XamlReader.Load(stream);
            }
#endif
        }
public override void OnApplyTemplate()
        {
base.OnApplyTemplate();
            _textBlock = GetTemplateChild("TextBlock") as TextBlock;
            CreateVerticalText(_text);
        }
private string _text { get; set; }
private TextBlock _textBlock { get; set; }
public string Text
        {
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
        }
public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
"Text", typeof(string), typeof(VerticalTextBlock), new PropertyMetadata(OnTextChanged));
private static void OnTextChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            ((VerticalTextBlock)o).OnTextChanged((string)(e.NewValue));
        }
private void OnTextChanged(string newValue)
        {
            CreateVerticalText(newValue);
        }
private void CreateVerticalText(string text)
        {
            _text = text;
if (null != _textBlock)
            {
bool first = true;
foreach (var c in _text)
                {
if (!first)
                    {
                        _textBlock.Inlines.Add(new LineBreak());
                    }
                    _textBlock.Inlines.Add(new Run { Text = c.ToString() });
                    first = false;
                }
            }
        }
    }

復制代碼

如何應用?

很簡單,添加控件所在的命名空間,然后再xaml里像添加控件一樣加載即可.

<mcl:VerticalTextBlock Text="功能列表" FontWeight="bold" Foreground="Red" FontSize="20"/>

效果:

第二種方法比較好,就像正常使用TextBlock控件一樣使用,不需要考慮到字體大小和控件大小間的關系.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM