WPF文字排列方式解析zz
2014-07-14 18:16
232 查看
WPF文字的处理是一个比较基础的技能。在使用WPF开发工具时,对于各种文字的处理时经常会遇到的情况。希望大家可以通过实践经验的积累,牢固掌握这一方面知识。
AD:WOT2014:用户标签系统与用户数据化运营培训专场
WPF文字在处理的过程中可以实现许多种方式来满足我们开发人员在实际编程中的需求。在这里将会为大家呈现一种WPF文字作为标题时的竖排方式。
WPF设备输入方式总结
WPF导出图片具体方式介绍
WPF视频使用方法详解
WPF类型转换器实际概念详解
WPF对话框相关实现方法介绍
有时Expande 控件的标题文字需要竖排,例如 Expande的FlowDirection属性为"RightToLeft",即左右方向的收。
WPF文字的处理相关代码示例:
< Grid x:Name="gridTemplate">
< Grid.Resources>
< !--模板数据的Expender标题竖排-->
< DataTemplate x:Key=
"ExpanderHeaderTextV">
< TextBlock Text="{Binding}"
Width="30"
Foreground="Green"
FontSize="20"
FontWeight="Normal"
TextWrapping="Wrap">
< TextBlock.RenderTransform>
< TransformGroup>
< MatrixTransform/>
< /TransformGroup>
< /TextBlock.RenderTransform>
< Run Text="模"/>
< LineBreak/>
< Run Text="版"/>
< LineBreak/>
< Run Text="内"/>
< LineBreak/>
< Run Text="容"/>
< LineBreak/>
< /TextBlock>
< /DataTemplate>
< /Grid.Resources>
< Expander HorizontalAlignment=
"Stretch" Header="" HeaderTemplate=
"{StaticResource ExpanderHeaderTextV}
" ExpandDirection="Left"
FlowDirection="RightToLeft"
VerticalAlignment="Stretch"
AllowDrop="False">
< TabControl IsSynchronizedWith
CurrentItem="True" Margin=
"0,0,0,0" FontSize="14">
< TabItem Header="模板数据"
x:Name="tabTemplate">
< Grid/>
< /TabItem>
< /TabControl>
< /Expander>
< /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控件一样使用,不需要考虑到字体大小和控件大小间的关系.
相关文章推荐
- MVVM模式解析和在WPF中的实现(六) 用依赖注入的方式配置ViewModel并注册消息
- [WPF打印]WPF 文档元素(Run TextBlock Paragraph)的文字对齐方式
- 折腾了一早上的C# WPF ListView+Grid 实现图片+文字 自动换行排列 类似Windows资源管理器效果
- C#利用GDI+绘制旋转文字,矩形内可以根据布局方式排列文本
- JavaScript--文字的排列方式
- MMS协议解析2(zz)
- 递归生成n个元素的所有排列方式
- ATi的救赎——X1000系列显卡技术详细解析2(zz)
- 操作XML的四种方式(zz)
- 在文字模式下用mail如何将文件以附件方式发送
- 一些关于GDI+的文字(zz)
- C复杂声明解析(ZZ)
- 文字的排列
- Windows快捷方式文件格式解析
- 排列字母ILLINOIS,字母NOS,共有多少种排列方式?
- Windows快捷方式文件格式解析
- 四种解析xml方式
- Eclipse基础--使用links方式安装Eclipse插件 zz
- SVN的Local方式:个人源码管理的好办法 (zz)
- 文字列を全角/半角に変換するには?(VB.NET関数活用) ZZ