繁体   English   中英

如何以编程方式设置HeaderedContentControl的HeaderTemplate属性

[英]How to set HeaderTemplate Property of HeaderedContentControl Programmatically

我在WPF工作,我需要设置HeaderedContentControl的Header的Text和ToolTip。 所以我要做的是创建一个模板如下:

 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;

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

希望有人能提供帮助,谢谢你提前

不知道为什么你这样使用模板。 为什么不用文本块设置header属性?

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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM