我有一个用户控件,后面有这段代码:
/// <summary>
/// The text to use for the header.
/// </summary>
public UIElement HeaderText
{
get { return (UIElement) GetValue(HeaderTextProperty); }
set { SetValue(HeaderTextProperty, value); }
}
public static DependencyProperty HeaderTextProperty =
DependencyProperty.Register("HeaderText",
typeof(UIElement),
typeof(Panel),
new PropertyMetadata(null, new PropertyChangedCallback(HeaderTextPropertyChanged)));
private static void HeaderTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var control = d as Panel;
if (control != null)
{
control.HeaderText = (UIElement) e.NewValue;
}
}
而这个 XAML:
<TextBlock Text="{TemplateBinding local:CustomPanel.HeaderText}" />
我正在尝试像这样使用用户控件:
<controls:CustomPanel>
<controls:CustomPanel.HeaderText>
<TextBlock Text="Foo " />
<TextBlock Text="{Binding Bar}" />
<TextBlock Text=" baz." />
</controls:CustomPanel.HeaderText>
</controls:CustomPanel>
但是,我得到的是空白/空文本。
UIElement
如果我更改为string
in code-behind,我可以让它工作,但我想接受这两者string
,TextBlock
以及几乎任何UIElement
对文本有意义的东西。
我怎样才能做到这一点?