
Untitled
By: a guest on
Apr 26th, 2012 | syntax:
None | size: 1.56 KB | hits: 17 | expires: Never
passing a TextBlock or collection of inlines to a child UserControl in WPF
<myNS:InfoBox Text="Some Text"/>
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeof(InfoBox),
new UIPropertyMetadata(null,ValueChanged));
private static void ValueChanged(DependencyObject dpo,
DependencyPropertyChangedEventArgs args)
{
((InfoBox)dpo).TextBlock.Text = (string)args.NewValue;
}
<myNS:InfoBox>
<myNS:InfoBox.ReferenceBlock>
<TextBlock>
<Run Language="en-gb" Text="SampleSample"/><LineBreak/>
<Run Language="en-gb"/><LineBreak/>
<Run Language="en-gb" Text="MoreMoreMore"/>
</TextBlock>
<myNS:InfoBox.ReferenceBlock>
</myNS:InfoBox>
public static readonly DependencyProperty ReferenceBlockProperty =
DependencyProperty.Register("ReferenceBlock", typeof(TextBlock),
typeof(InfoBox), new UIPropertyMetadata(null, ReferenceBlockReceived));
[...]
private static void ReferenceBlockReceived(DependencyObject dpo,
DependencyPropertyChangedEventArgs args)
{
var textblock = (TextBlock)args.NewValue;
if (textblock != null)
{
((InfoBox)dpo).TextBlock.Inlines.Clear();
((InfoBox)dpo).TextBlock.Inlines.AddRange(textblock.Inlines);
}
}
<ControlTemplate TargetType="myNS:InfoBox">
<ContentControl Content="{TemplateBinding ReferenceBlock}" />
</ControlTemplate>