您還可以使用可綁定運行。有用的東西,尤其是如果你想添加一些文本格式(顏色,fontweight等)。
<TextBlock>
<something:BindableRun BoundText="{Binding Number}"/>
<Run Text=" - "/>
<something:BindableRun BoundText="{Binding Name}"/>
</TextBlock>
Here's一個原始類:
Here有一些額外的改進。
而這一切,在一個代碼片段:
public class BindableRun : Run
{
public static readonly DependencyProperty BoundTextProperty = DependencyProperty.Register("BoundText", typeof(string), typeof(BindableRun), new PropertyMetadata(new PropertyChangedCallback(BindableRun.onBoundTextChanged)));
private static void onBoundTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((Run)d).Text = (string)e.NewValue;
}
public String BoundText
{
get { return (string)GetValue(BoundTextProperty); }
set { SetValue(BoundTextProperty, value); }
}
public BindableRun()
: base()
{
Binding b = new Binding("DataContext");
b.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(FrameworkElement), 1);
this.SetBinding(DataContextProperty, b);
}
}
而不是您可以使用的空間{} StringFormat =「{} {0} - {1}」 – 2009-02-12 19:11:23
謝謝,不知道! :) – PiRX 2009-02-12 19:54:36