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里的实现也是调用这个静态方法实现的。

posted @ 2011-06-26 21:50  超时空饭盒  阅读(2426)  评论(1编辑  收藏  举报