【发布时间】:2012-02-24 16:43:24
【问题描述】:
我创建了一个名为 InfoBox 的用户控件,它的作用类似于一个精美的文本块(额外的按钮等)。它工作正常。我可以像这样在 Blend 中使用它:
<myNS:InfoBox Text="Some Text"/>
其中'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;
}
当我在 Blend 中添加控件时,它会显示其设计时示例文本,直到我指定 Text="Something",在这种情况下,“Something”会神奇地出现在设计器中。完美!
但现在我想传递的不仅仅是文本,我希望能够使用你在文本块中内联的所有时髦功能。运行、斜体等...
为什么以下不起作用?
<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);
}
}
由于某种原因,处理程序接收到的 TextBlock 完全为空。我很感激任何帮助。这 WPF 的东西很难!
【问题讨论】:
标签: wpf user-controls wpf-controls dependency-properties textblock