0

我在 WPF 中工作,我需要设置HeaderedContentControl的标题的文本和工具提示。所以我想做的是创建一个模板,如下所示:

 System.Windows.DataTemplate template = new System.Windows.DataTemplate();
 template.DataType = typeof(HeaderedContentControl);

 System.Windows.FrameworkElementFactory blockFactory = new System.Windows.FrameworkElementFactory(typeof(TextBlock));
 blockFactory.SetValue(TextBlock.TextProperty, "The Header Text");
 blockFactory.SetValue(TextBlock.ToolTipProperty, "The ToolTip");

 template.VisualTree = blockFactory;

 myHeaderedContentControl.HeaderTemplate = template;

但是当我运行程序时,标题显示为空。我究竟做错了什么?

希望有人可以帮助,提前谢谢

4

1 回答 1

2

不知道为什么要以这种方式使用模板。为什么不使用文本块设置标题属性?

myHeaderedContentControl.Header = new TextBlock 
{
    Text = "Some text", 
    ToolTip = "Some tooltip"
};

此外,最好在 XAML 中定义所有内容:

<HeaderedContentControl x:Name="control">
    <HeaderedContentControl.Header>
        <TextBlock Text="Some text" ToolTip="Some tooltip"/>
    </HeaderedContentControl.Header>
</HeaderedContentControl>
于 2012-06-22T21:04:03.043 回答