以编程方式覆盖工具提示模板

时间:2016-03-31 15:28:11

标签: wpf templates tooltip

我想以编程方式覆盖Tooltip Template。 等效的xaml代码是:

<Style TargetType="ToolTip">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ToolTip">
                <ContentPresenter />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我的尝试:

 ToolTip tooltip = new ToolTip();
 tooltip.Content = panel; // Panel with text.
 var controlTemplate = new ControlTemplate(typeof(ToolTip));
 tooltip.Template = controlTemplate;
 // ............. -> ContentPresenter ??

1 个答案:

答案 0 :(得分:1)

ToolTip tooltip = new ToolTip();
tooltip.Content = panel; // Panel with text.
var controlTemplate = new ControlTemplate(typeof(ToolTip));
controlTemplate.VisualTree = new FrameworkElementFactory(typeof(ContentPresenter));
tooltip.Template = controlTemplate;