简体   繁体   English

WPF自定义控件,以内联方式或在弹出窗口中显示内容

[英]WPF Custom Control That Displays Content Inline or in a Popup

I am creating a custom WPF content control that has a DisplayMode property which can be: 我正在创建一个具有DisplayMode属性的自定义WPF内容控件,该属性可以是:

  • Inline 排队
  • Popup 弹出

When DisplayMode="Inline", my ControlTemplate can use a standard ContentPresenter like normal. 当DisplayMode =“ Inline”时,我的ControlTemplate可以像通常一样使用标准的ContentPresenter。

However, when DisplayMode="Popup", I want the Content to be displayed in a Popup control. 但是,当DisplayMode =“ Popup”时,我希望Content显示在Popup控件中。

How should I solve this problem? 我应该如何解决这个问题?

Does it have to happen purely in code when the DisplayMode property changes? DisplayMode属性更改时,它是否必须纯粹在代码中发生? How do I move the content of the Content property between a ContentPresenter and the Popup? 如何在ContentPresenter和弹出窗口之间移动Content属性的内容?

It looks like I was trying to make this more complex than it really is. 看起来我正在尝试使它变得比实际更复杂。

The solution to this was to create two separate ControlTemplate(s). 解决方案是创建两个单独的ControlTemplate。 One that displays inline and one that displays in a Popup control. 一个显示内联,另一个显示在Popup控件中。

Next, all I had to do was create a couple style triggers that change the ControlTemplate based on the value of the DisplayMode property. 接下来,我要做的就是创建几个样式触发器,这些触发器将根据DisplayMode属性的值更改ControlTemplate。

It looks like this: 看起来像这样:

<Style x:Key="MyControlStyle" TargetType="{x:Type my:MyControl}">
    <Setter Property="Template" Value="{StaticResource InlineTemplate}"/>
    <Style.Triggers>
        <DataTrigger Binding="{Binding DisplayMode}" Value="Inline">
            <Setter Property="Template" Value="{StaticResource InlineTemplate}"/>
        </DataTrigger>
        <DataTrigger Binding="{Binding DisplayMode}" Value="Overlay">
            <Setter Property="Template" Value="{StaticResource OverlayTemplate}"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

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

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