Silverlight4-Behavior-TextBlock竖直排列文字行为

对于TextBlock我们有时候需要竖直排列它的文字内容。

设置TextBlock的显示内容有两种方式,一是设置它的Text属性,二是往它的InlineCollection里添加内容。

可以发现我们能同时设置Text属性,并添加InlineCollection信息,但最后显示的会是InlineCollection里的信息,并且不会对Text属性的值产生影响。

那么我们是否可以把文本值赋给Text属性,然后再获取相关值并在InlineCollection里进行竖直设置呢。

那么我们需要获取TextBlock的TextChanged事件,可惜这个事件并不存在。

public class VerticalTextBlockBehavior : Behavior < TextBlock >
{
protected override void OnAttached()
{
base .OnAttached();

BindingOperations.SetBinding(
this , VerticalTextBlockBehavior.InternalTextProperty, new Binding( " Text " ) { Source = this .AssociatedObject });
this .VerticalText();
}

protected override void OnDetaching()
{
base .OnDetaching();

this .ClearValue(VerticalTextBlockBehavior.InternalTextProperty);
}

private void VerticalText()
{
var text
= this .AssociatedObject.Text;
this .AssociatedObject.Inlines.Clear();

if ( ! string .IsNullOrEmpty(text))
{
foreach (var word in text)
{
this .AssociatedObject.Inlines.Add( new Run { Text = word.ToString() });
this .AssociatedObject.Inlines.Add( new LineBreak());
}
this .AssociatedObject.Inlines.RemoveAt(text.Length * 2 - 1 );
}
}

private string InternalText
{
get { return ( string )GetValue(InternalTextProperty); }
set { SetValue(InternalTextProperty, value); }
}

// Using a DependencyProperty as the backing store for InternalText. This enables animation, styling, binding, etc...
private static readonly DependencyProperty InternalTextProperty =
DependencyProperty.Register(
" InternalText " , typeof ( string ), typeof (VerticalTextBlockBehavior),
new PropertyMetadata(OnInternalTextChanged));

private static void OnInternalTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var source
= d as VerticalTextBlockBehavior;
source.VerticalText();
}
}

我们在行为内部添加一个动态属性,因为这个属性并不需要对外公开,所以都设成Private就行了。然后在OnAttached方法里把这个属性绑定到TextBlock的Txet属性,这样当TextBlock的Text属性发生变化时,我们这个内部属性也会跟着变化,并最终回调到行为内部的方法VerticalText

VerticalText里的处理很简单,获取Text内容,然后每隔一个字符都插入一个断行,这样就会产生竖直排列文本的效果了。

PS:很多童鞋都知道控件后台代码绑定是通过FrameworkElement.SetBinding(DependencyProperty dp, Binding binding)方法来实现的,而对于不是继承自FrameworkElement的类却不知道如何处理了,其实就是BindingOperations.SetBinding(DependencyObject target, DependencyProperty dp, BindingBase binding),而FrameworkElement.SetBinding里的实现也是调用这个静态方法实现的。

转载于:https://www.cnblogs.com/HalfwayMonk/archive/2011/06/26/2090902.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值