简体   繁体   中英

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:

  • Inline
  • Popup

When DisplayMode="Inline", my ControlTemplate can use a standard ContentPresenter like normal.

However, when DisplayMode="Popup", I want the Content to be displayed in a Popup control.

How should I solve this problem?

Does it have to happen purely in code when the DisplayMode property changes? How do I move the content of the Content property between a ContentPresenter and the Popup?

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). One that displays inline and one that displays in a Popup control.

Next, all I had to do was create a couple style triggers that change the ControlTemplate based on the value of the DisplayMode property.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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